ViewBase.js 1.5 KB

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