Ember - 无法设置null的属性'type'

时间:2013-10-18 11:31:32

标签: ember.js routing

我对ember有一个大问题。如果我导航到某个地方,我有时会收到错误Cannot set property 'type' of null,并且不会呈现视图。错误是在jQuery中抛出的,而不是在Ember中。

我从未在我的应用程序中设置something.type。我不知道错误是什么。路线+控制器+视图的示例(不要忘记,每次都不会发生。)

路由:this.route("channels", {path: "/channels/:only"});

路线:

App.ChannelsRoute = Ember.Route.extend({
  model: function(params) {
    var controller = this.controllerFor("channels");
    controller.set("only", params.only);

    if (!controller.get("hasContent")) {
      return controller.updateContent();
    }
  }
});

控制器:

App.ChannelsController = Ember.ArrayController.extend({

  sortProperties: ["position"],
  sortAscending: true,

  hasContent: function() {
    return this.get("content").length > 0;
  }.property("@each"),

  channels_category: function() {
    return this.get("only");
  }.property("only"),

  filteredContent: function() {
    var filterType = "group";
    var filterID = 1;

    switch(this.get("only")) {
      case "primary": filterType = "group"; filterID = 1; break;
      case "secondary": filterType = "group"; filterID = 2; break;
      case "regional": filterType = "group"; filterID = 3; break;
      case "hd": filterType = "hd"; filterID = true; break;
      default: filterType = "group"; filterID = 1; break;
    }

    return this.filterProperty(filterType, filterID);
  }.property("@each", "only"),

  updateContent: function() {
    return $.getJSON(getAPIUrl("channels.json")).then(function(data){
      var channels = [];
      $.each(data.channels, function() {

        var channel = App.Channel.create({
          id: this.id,
          name: this.name,
          group: this.group,
          hd: this.hd,
          position: this.position,
          recordable: this.recordable
        });
        channels.pushObject(channel);
      });

      return channels;
    });
  }
});

它发生在很多控制器中。我希望有人知道解决方案。

2 个答案:

答案 0 :(得分:0)

您可以测试updateContent函数的这种变体吗?将渠道变更改为控制器模型......

  updateContent: function() {
    return $.getJSON(getAPIUrl("channels.json")).then(function(data){
      var channels = this.get('model');
      $.each(data.channels, function() {

        var channel = App.Channel.create({
          id: this.id,
          name: this.name,
          group: this.group,
          hd: this.hd,
          position: this.position,
          recordable: this.recordable
        });
        channels.pushObject(channel);
      });
    });
  }

答案 1 :(得分:0)

如果它发生在jQuery中,它发生在AJAX调用或你正在用jQuery做的一些DOM操作中。你最好的开始是使用你的AJAX调用,并检查你正在使用的任何Handlebars助手(特别是如果你声明了任何类型的input helpers)。