ViewBase.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. cc.Class({
  2. extends: cc.Component,
  3. properties: {
  4. },
  5. initView(param) {
  6. },
  7. createComponent(type, parent, param, cb, cache = false) {
  8. let prefabPath = G.UIMgr.defaultUIPath + JMC.UIPath[type];
  9. this.creawteUI(prefabPath, parent, param, cb, cache)
  10. },
  11. creawteUI(prefabPath, parent, param, cb, cache = false) {
  12. G.LogUtils.warn('showAlert', prefabPath);
  13. let self = this;
  14. cc.loader.loadRes(prefabPath, cc.Prefab, (completedCount, totalCount, item) => {
  15. }, (err, prefab) => {
  16. if (err) {
  17. G.AppUtils.getSceneCtrl().addToast('网络异常!');
  18. G.LogUtils.error('[showAlert]', err);
  19. if (cb) {
  20. cb(undefined, 'error', err);
  21. }
  22. return;
  23. }
  24. let node = cc.instantiate(prefab);
  25. let ret = self.safeAddChild(parent, node);
  26. if (!ret) {
  27. return;
  28. }
  29. let viewBase = node.getComponent("ViewBase");
  30. if (viewBase) {
  31. viewBase.initView(param)
  32. }
  33. if (!cache) {
  34. cc.loader.releaseRes(prefabPath);
  35. }
  36. if (cb) {
  37. cb(node, 'ok', undefined);
  38. }
  39. });
  40. },
  41. safeAddChild(parent, node) {
  42. if (!cc.isValid(node) || !cc.isValid(parent)) {
  43. return false;
  44. }
  45. parent.addChild(node);
  46. cc.game.emit('e_ui_push_alert', {node});
  47. return true;
  48. },
  49. });