0

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

  });

and a collection:

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

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

and log:

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?

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

1
  • obviously i know it's not an empty object but if i use the toJSON() method it actually returns empty array. Sorry for the confusion! Commented Apr 26, 2013 at 21:09

1 Answer 1

1

The Collection#fetch behavior has changed, it used to reset the collection by default but as of 1.0.0 it merges the new models using set:

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 });
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you for the answer but this is not the point for two reasons: i'm on backbone 0.9.2 and fetch method actually fires a 'reset' event (otherwise it wouldn't log anything). Just to be sure i tried your way (both fetch({reset:true}) and add,remove,change event binding) but it doesn't solve the problem.
@user1203806: Are you really doing books.on('reset', this.log()); rather than books.on('reset', this.log);? You're not supposed to call the method when binding to an event, you're supposed to pass the method itself.
oh god, i feel so stupid... thank you! So, if i do this.log() the log method is evaluated just after the binding itself get evaluated, instead if i do this.log it wait for the reset event to be fired, did i understand? i tried to search on stackoverflow, but i don't know what keywords to use.
If you do books.on('reset', this.log()) then log is called before books.on is called, log is called while building the argument list for the books.on call; if you books.on('reset', this.log) then log is called when the 'reset' happens. Don't feel too bad, I didn't spot that either.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.