骨干+ Parse.com Collection.fetch()使用事件回调返回空数组 [英] Backbone+Parse.com Collection.fetch() returns empty array using event callback

查看:156
本文介绍了骨干+ Parse.com Collection.fetch()使用事件回调返回空数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用parse.com开发一个web应用程序开始,但我被困在一个简单的问题。
我定义的模型(或解析SDK对象)为:

i'm starting using parse.com to develop a web app but i'm stuck on a simple problem. I defined a model (or object in Parse SDK) as:

Book.Model = Parse.Object.extend("book", {
    // Default attributes for the book.
    defaults: {
      title: "placeholder...",
    },

    // Ensure that each book created has `title`.
    initialize: function() {
      if (!this.get("title")) {
        this.set({"title": this.defaults.title});
      }
    },

  });

和集合:

Book.List = Parse.Collection.extend({

    // Reference to this collection's model.
    model: Book.Model,

    initialize: function() {
    },

  });

然后,如果我尝试像

Then, if i try something like

   var books = new Book.List();
   books.fetch({
        success: function(collection) {
            console.warn(collection);
        },
        error: function(collection, error) {
           // The collection could not be retrieved.
        }
    });

一切顺利。日志:

Everything goes fine. Log:

child {length: 5, models: Array[5], _byId: Object, _byCid: Object, model: function…}
_byCid: Object
_byId: Object
length: 5
models: Array[5]
__proto__: EmptyConstructor

但是,如果我尝试使用事件回调,而不是成功的方法,我得到一个空数组。 code:

BUT if i try to use event callback instead of success method i get an empty array. Code:

books.on('reset', this.log());
books.fetch();

    log: function() {
      console.log(books);
    }

和日志:

child {length: 0, models: Array[0], _byId: Object, _byCid: Object, model: function…}
_byCid: Object
_byId: Object
length: 5
models: Array[5]
__proto__: EmptyConstructor

这是很奇怪的(因为我认为每个解决方案等待集合从服务器填充)。是否有人知道为什么会这样?

which is quite strange (because i think that each solution wait for the collection to be populated from the server). Does anybody know why is this happening?

我实际使用骨干样板和Parse.com JS SDK。

I'm actually using Backbone Boilerplate and Parse.com js SDK.

推荐答案

收藏#取 行为发生了变化,它默认用于收集重置但作为 1.0 0.0,它把新车型使用设置

在从服务器模型数据的回报,它使用的设置导入(智能)合并所取模型,除非你通过 {重置:真正} [...]

When the model data returns from the server, it uses set to (intelligently) merge the fetched models, unless you pass {reset: true}, [...]

设置 不会触发复位事件,它会触发其他事件:

and set doesn't trigger "reset" events, it triggers other events:

所有适当的添加删除的变化事件被解雇,因为这会发生。

All of the appropriate "add", "remove", and "change" events are fired as this happens.

如果你希望你的重置集合,那么你必须这么说:

If you want your fetch to reset the collection then you have to say so:

books.fetch({ reset: true });

这篇关于骨干+ Parse.com Collection.fetch()使用事件回调返回空数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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