骨干自举收集不正确初始化

编程入门 行业动态 更新时间:2024-10-26 05:31:36
本文介绍了骨干自举收集不正确初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

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.

在backbonejs/#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}); //... };

一步一步:

  • this._reset() 通话做了 this.length = 0 。
  • 的 this.initialize.apply(...)是调用你的初始化方法。
  • this.reset(... ) 将调用 添加 添加的车型。在添加通话将更新集合的模式和长度属性
  • The this._reset() call does a this.length = 0.
  • The this.initialize.apply(...) is the call to your initialize method.
  • 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);

    的 参数 在那里意味着初始化将获得相同的参数的构造函数所以你可以看看在那里,如果你想要的东西:

    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. }

    更多推荐

    骨干自举收集不正确初始化

    本文发布于:2023-11-10 06:07:56,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1574558.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:初始化   不正确   骨干

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!