骨干自举收集不正确初始化 [英] Backbone bootstrapped collection doesn't initialize correctly

查看:164
本文介绍了骨干自举收集不正确初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,那真的很难发现,因为在大多数情况一切正常。只有当我试图操纵我在我的收藏数据初始化函数,我发现一个问题。

I have an issue, that was really hard to notice, because for the most part everything works. It was only when I tried to manipulate my data in my collections initialize function that I found a problem.

在<一个骨干文档href=\"http://backbonejs.org/#Collection-constructor\">http://backbonejs.org/#Collection-constructor

如果你定义一个初始化函数,创建集合时,它会被调用。

"If you define an initialize function, it will be invoked when the collection is created."

所以我间preTED,作为我的初始化函数将不运行,直到后,我的模型设定。 这听起来很理想,我说,但后来我遇到了这一点。

so I interpreted that as, my initialize function won't run until after my models are set. "That sounds ideal," said I, but then I ran into this.

我的引导code是如下:

My bootstrap code is as follows:

new MyCollection(<?php if ($data) {echo json_encode($data);} ?>);

我的集合:

var MyCollection = Backbone.Collection.extend({
    model: MyModel,

    initialize: function() { 
        console.log(this); 
        console.log(this.length);
        this.each(function(model) {
            console.log(model);
        });
    } 
});  

我得到了奇怪的结果。

I got strange results.

第一个的console.log(本); 预料的一样集合对象:

The first console.log(this); was a collection object as expected:

{ 
    .... 
    models: [3], 
    length: 3 
    .... 
} 

和第二控制台(this.length); 打印出号 0

控制台this.each()没有露面。

这是怎么回事?

推荐答案

收集构造的看起来像这样

var Collection = Backbone.Collection = function(models, options) {
  //...
  this._reset();
  this.initialize.apply(this, arguments);
  //...
  this.reset(models, {silent: true, parse: options.parse});
  //...
};

一步一步:


  1. this._reset() 通话做了 this.length = 0

  2. this.initialize.apply(...)是调用你的初始化方法。

  3. this.reset(... ) 将调用 添加 添加的车型。在添加通话将更新集合的模式长度属性

  1. The this._reset() call does a this.length = 0.
  2. The this.initialize.apply(...) is the call to your initialize method.
  3. The this.reset(...) will call add to add the models. The add call will update the collection's models and length properties.

所以,当初始化被调用时,你就会有 this.length = = 0 this.models 将因为只有 _reset 将在这里被称为空数组。现在,我们可以很容易地看到为什么 this.each 没有做任何事情,为什么的console.log(this.length) 0

So, when initialize is called, you'll have this.length == 0 and this.models will be an empty array since only _reset will have been called here. Now we can easily see why this.each doesn't do anything and why console.log(this.length) says 0.

但为什么的console.log(本)告诉我们,我们有一个人口稠密的收藏?好吧,的console.log 不会立即发生,它只是抓住它的参数引用,并记录一些东西到控制台有点晚;由时间的console.log 被周围的东西放在控制台,你必须通过上面的(3)得到,这意味着,你马上有 this.models this.length ,你希望看到的。如果你说

But why does console.log(this) tell us that we have a populated collection? Well, console.log doesn't happen right away, it just grabs references to its arguments and logs something to the console a little bit later; by the time console.log gets around to putting something in the console, you'll have gotten through (3) above and that means that you'll have the this.models and this.length that you're expecting to see. If you say

console.log(this.toJSON());

console.log(_(this.models).clone())

你会看到事物的状态时,的console.log 叫,而不是事物的状态时,的console.log 写入控制台。

you'll see the state of things when console.log is called rather than the state of things when console.log writes to the console.

该文档是不完全明确哪些应该是准备好初始化被称为所以你坚持通过溯源。这并不理想,但至少骨干源是干净的,直线前进。

The documentation isn't exactly explicit about what is supposed to be ready when initialize is called so you're stuck tracing through the source. This isn't ideal but at least the Backbone source is clean and straight forward.

您会注意到初始化被称为是这样的:

You'll notice that initialize is called like this:

this.initialize.apply(this, arguments);

的<一个href=\"https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Functions_and_function_scope/arguments\"相对=nofollow> 参数 在那里意味着初始化将获得相同的参数的构造函数所以你可以看看在那里,如果你想要的东西:

The arguments in there means that initialize will receive the same arguments as the constructor so you could look in there if you wanted:

initialize: function(models, options) {
    // The raw model data will be in `models` so do what
    // needs to be done.
}

这篇关于骨干自举收集不正确初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆