1

So far i have the following code, but it doesn't seem to be working, and I don't know when the asynchronous fetch is completed:

var item = Backbone.Model.extend({
    defaults: {
        id: 0,
        an_id: 0,
        strval: null,
        th_id: 0,
        text: null
    },
    url: 'page.php',
    options: {
        success: function(data) {
            alert('s: ' + dump(data));
            // the dump function is my way of dumping objects into a string,
            // use console.log if you want, as I have that disabled
        },
        error: function(x, t, e) {
            alert('e: ' + t + ', ' + e);
        }
    }
});

var coll = Backbone.Collection.extend({
    model: item
});

var options = new Options();
Backbone.sync("create", coll, item.options); // 'undefined' is not an object (evaluating c.url) in backbone-min.js

Update

I have modified the original code to what I have now, and the backend can now tell the difference between New, Update, Save and Delete requests.

I still cannot find out how to populate the collection coll.

2
  • It depends on what you want to do. Generally you'll want to hook a view up to your model and bind your view's render method to events that you model fires. If you're fetching, bind the reset event to your views render as reset will fire once the response returns from the server. Commented Feb 8, 2012 at 21:19
  • @kinakuta, I have no idea how to do any of that, thanks! Commented Feb 8, 2012 at 21:42

2 Answers 2

1

Backbone.Collection is for keeping multiple items - you seem to be trying to have your Collection "inherit" from your model, which isn't the right approach.

Collections are ordered sets of models. You can bind "change" events to be notified when any model in the collection has been modified, listen for "add" and "remove" events, fetch the collection from the server, and use a full suite of Underscore.js methods.

Sign up to request clarification or add additional context in comments.

2 Comments

As I pointed out, I have only started using this library. Do you have a definitive suggestion on how to store a list models (properly) and listen for change, add and remove events and get a list of the available collection?
I have updated my code, how can I populate my collection of items?
0

you can add a success handler to your fetch call. try this:

coll.fetch({
        success: function() {
            alert("success");
            console.log(coll.toJSON());
        },
        error: function(){
            alert("error")}
    });    

3 Comments

The success handler is called, but the toJSON() function displays an empty string.
sounds like your api call isn't configured correctly. I'd ask if you can go into the developer tool in your browser to see what network calls are made, see what your call is returning. also a useful tool to test api calls is this: (for chrome) google.com/…
I have updated my code, how can I popualte my collection of items?

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.