Allan Raquin
Allan Raquin

Reputation: 613

Dynamically change backbone model

Here my problem, I want to change my model dynamically (change dynamically a variable in my model when the collection is instantiated).

So here my code :

define(['backbone'], function (backbone) {

  var MyModel = Backbone.Model.extend({
    initialize: function() {
        var that = this;
        var likes;
        var UrlGetLike = "https://api.facebook.com/method/fql.query?query=select%20like_count%20from%20link_stat%20where%20url=%27https://www.facebook.com/pages/Stackoverflow/1462865420609264%27&format=json";
        $.getJSON( UrlGetLike, {
            format: "json"
        })
        .done(function(data) {
            likes = data[0].like_count;
            that.set({
                'likes' : likes
            });
        });
    },
  });
return MyModel;
});

But the data are not updated, MyModel is returned before the .done() finished ..

I try this too :

define(['backbone'], function (backbone) {

  var MyModel = Backbone.Model.extend({
    initialize: function() {
        var that = this;
        var likes;
        var UrlGetLike = "https://api.facebook.com/method/fql.query?query=select%20like_count%20from%20link_stat%20where%20url=%27https://www.facebook.com/pages/Stackoverflow/1462865420609264%27&format=json";
        $.getJSON( UrlGetLike, {
            format: "json"
        })
        .done(function(data) {
            likes = data[0].like_count;
            that.set({
                'likes' : likes
            });
            that.returnn;
        });
    },
    returnn: function(){
        return this;
    }
  });
});

But I got this error Cannot read property 'prototype' of undefined, because I fired

var collection = new Collection({collection : MyModel});

before MyModel is return (I think)

If anyone have a solution or something to help me, it would be appreciate :).

Upvotes: 0

Views: 200

Answers (1)

Mironor
Mironor

Reputation: 1187

You can fetch the info for each model in the collection after its creation (it's actually a bad thing to fetch data in initialize method as this method was not created for that purpose. It's better to call a fetch method for a model explicitly (in our case let's call it fetchLikes))

  var MyModel = Backbone.Model.extend({
      fetchLikes: function () {
          var UrlGetLike = "https://api.facebook.com/method/fql.query?query=select%20like_count%20from%20link_stat%20where%20url=%27https://www.facebook.com/pages/Stackoverflow/1462865420609264%27&format=json";
          $.getJSON(UrlGetLike, {
              format: "json"
          }, _.bind(function (data) {
              likes = data[0].like_count;
              that.set({
                  'likes': likes
              });
          }, this));
      }
  });

  var Collection = Backbone.Collection.extend({
      model: MyModel
  })

   var collection = new Collection();

  //.. insert models in the colleciton ..

  collection.forEach(function (model) {
      model.fetchLikes();
  })

Bare in mind that you are doing ajax requests in for-loop that is considered a bad practice. Do it only if you have no way to get the whole data in one request.

Upvotes: 1

Related Questions