Emberjs布局错误

时间:2012-03-16 10:50:40

标签: javascript ember.js

您已经开始使用emberjs开发一个应用程序到目前为止一切都很好,我注意到了一件事,当我刷新网页时,不时显示任何内容,我在浏览器控制台中收到以下错误: 未捕获的TypeError:无法读取undefined的属性'layoutStates' - ember-layout.js

如果我再次刷新页面,则错误消失,

为什么会出现此错误? 如何阻止此错误发生?

提前致谢...

请求的路由管理器代码

App.routeManager = em.RouteManager.create({
    rootView: App.main,
    home: em.LayoutState.create({
        selector: '.inbox',
        viewClass: App.InboxView,
        enter: function (stateManager, transition) {
            this._super(stateManager, transition);
        }
    }),
    View2: em.LayoutState.create({
        route: 'contacts',
        selector: '.contacts',
        viewClass: App.ContactsView,
        enter: function (stateManager, transition) {
            this._super(stateManager, transition);
        }
    }),
    View3: em.LayoutState.create({
        route: 'account',
        selector: '.account',
        viewClass: App.AccountView,
        enter: function (stateManager, transition) {
            this._super(stateManager, transition);
        }
    })
});

错误发生在ember-layouts.js的第126行

 init: function() {
    // This is currently experimental. We allow
    // the view itself to define it's substates
    // for better encapsulation. To do this, set
    // the layoutStates property.
    var viewClass = get(this, 'viewClass');
    if(viewClass) {
      var layoutStates = get(viewClass, 'proto').layoutStates;
      set(this, 'states', layoutStates);
    }

     this._super();
   },

2 个答案:

答案 0 :(得分:0)

发现问题,它与ember-layouts无关,它与require js有关。在文件的顶部,我不得不用define替换require,它在require js api documentation http://requirejs.org/docs/api.html中解释。

 //------THIS ONE IS WRONG AND CAUSES THE ERROR ------------------
 require(['../../lib/ember/load-ember','model/notificationModel'], 
         function (em, notification) {});


 //Changing to define has fixed the problem in all browsers
 define(['../../lib/ember/load-ember','model/notificationModel'], 
         function (em, notification) {});

答案 1 :(得分:0)

我收到同样的错误,但我没有使用requirejs。

我认为我的问题是版本问题。我正在使用ember-0.9.5.js。我已经搜索过,原始函数似乎已从此版本中删除。 proto()确实存在于以前的版本中。

ember-layouts在第29行显式调用此函数

  var layoutStates = viewClass.proto().layoutStates;

将此行更改为此似乎可以解决问题

var layoutStates = viewClass.prototype.layoutStates;