【问题标题】:Ember - Cannot set property 'type' of nullEmber - 无法将属性“类型”设置为 null
【发布时间】:2013-10-18 11:31:32
【问题描述】:

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

我从未在我的应用程序中设置something.type。我不知道错误可能是什么。 Route + Controller + View 的示例(不要忘记,它不是每次都发生。)

路由: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;
    });
  }
});

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

【问题讨论】:

    标签: ember.js routing


    【解决方案1】:

    您能测试一下您的 updateContent 函数的这种变体吗?将频道 var 更改为控制器模型...

      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);
          });
        });
      }
    

    【讨论】:

    • 感谢您的回答,是的,我会在星期一对其进行测试并给您反馈! ;)
    • 嗨,很抱歉回答迟了。我试过了,如果我改变路线,它的工作原理。但是,如果我在这条路线上重新加载页面,应用程序将永远停留在加载路线中。控制台输出为空。我认为加载路线预计任何内容都会在之后返回。我不知道。 编辑:如果我在 updateContent 的末尾添加return [];,它会再次加载,但内容当然是空的。
    【解决方案2】:

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

    【讨论】:

    • 好吧,很高兴知道,我将创建一个小应用程序,看看它是否也在那里发生。我会在星期一试试!
    猜你喜欢
    • 1970-01-01
    • 2019-12-10
    • 1970-01-01
    • 2014-12-18
    • 2014-08-25
    • 2013-04-21
    • 2013-08-16
    • 2017-05-05
    • 2021-10-02
    相关资源
    最近更新 更多