-1

In my backbone function, while the name get change the change function not at all triggering.. any one suggest me the right way to get it.. (actually i need to get changed stuff and need to update);

code :

    (function($){

var list = {};

list.model = Backbone.Model.extend({
  defaults:{
    name:'need the name'
  },
  initialize:function(){
    this.bind('change:name', function(model) {
        console.log('Model->change()', model);
    });
  }
});

list.collect = Backbone.Collection.extend({
  model:list.model,
  url : 'data/names.json',
  initialize:function(){
    this.fetch({update:true});
    this.keepUpdate();
  },
  keepUpdate:function(){
    var that = this;
    var updateData = function(){
      that.fetch({update:true});
      myTimeout = setTimeout(updateData,10000);
    }
    var myTimeout = setTimeout(updateData,10000);
  }
});


list.view = Backbone.View.extend({
  initialize:function(){
    this.collection = new list.collect();
    this.collection.on("update", this.render, this);
    this.collection.bind("change:name", function(model, attributes){
      console.log(model,attributes,'property changed'); // this is not triggering at all..
    });
  },
  render:function(data){
    _.each(this.collection.models, function(data){
      //console.log(data.get('name')); it works fine
    })
  },
  updateName:function(){
    console.log('updated called');
  }
});

var newView = new list.view();    

})(jQuery)
3gwebtrain
  • 14,640
  • 25
  • 121
  • 247
  • Proper title please, summarizing the question. – Beetroot-Beetroot Jan 22 '13 at 11:51
  • edit your title. mention your problem there. title is meant for that, not the effort you put up on it – Ravi Gadag Jan 22 '13 at 11:51
  • Please don't keep spamming the same question over and over again. This is the third time you ask the same question today. – jevakallio Jan 22 '13 at 11:59
  • If you want to increase the exposure you can put a bounty on the question, but the most obvious would be to improve the clarity of the question, especially if you are looking for a "clear" answer. – eandersson Jan 22 '13 at 12:01
  • I suggest you take time to read the documentation for Collection.fetch: http://backbonejs.org/#Collection-fetch . This is the fourth time you open the same question, and in each one you try a different, incorrect way of fixing the same thing. The documentation tells you what options the `fetch` method accepts, and also which events it triggers. – jevakallio Jan 22 '13 at 15:23

3 Answers3

1

Collection.fetch doesn't trigger the change event. You only get the reset event. If you need more granular events, consider calling fetch with the options {update:true}.

that.fetch({update:true});

That will trigger change event for every model that was already in the collection, and add if the model was previously not in the collection.

jevakallio
  • 35,324
  • 3
  • 105
  • 112
  • nope, i unable to follow you.. can you update my code please.. i am very sorry to say this. – 3gwebtrain Jan 22 '13 at 07:29
  • @3gwebtrain, I edited my answer to be slightly more clear (wrote the original one the phone). But no, I am very sorry to say, I can't update your code for you. – jevakallio Jan 22 '13 at 07:36
  • let me update my total function again.. i am not get triggered by change event still.. – 3gwebtrain Jan 22 '13 at 09:14
  • @3gwebtrain, that's because there is not such thing as an `update` event... maybe if you actually read the answer and the corresponding (linked) documentation, you might have a higher chance of success. – jevakallio Jan 22 '13 at 09:44
0

Try removing keepUpdate from the collection and put a setTimeout in the initialize function of the view at the end. I suggest that fetch is called from the view as well as this.collection.fetch() instead of the collection's initialize function. Makes your code more reusable.

Xerri
  • 4,916
  • 6
  • 45
  • 54
0

I'm not sure I understand your question. What are you trying to achieve ?

I don't think that fetch accepts {add:true} as a parameter (I just checked the source code and it does not appear anywhere).

When fetch completes, it only triggers a reset event (not an add). You should listen to that if you want to do something when the content of the collection changes. You can also simplify listen to change.

Laurent Perrin
  • 14,671
  • 5
  • 50
  • 49