Backbonejs simplest collection fetch
Date : March 29 2020, 07:55 AM
this one helps. Backbone does not support fetching XML, hence, you'll need to override the sync method to provide your own custom parsing functionality. If you don't want to have to mess with Backbone internals, try doing your $.ajax GET first, parse your response into a proper JSON Array and then use that array with a Backbone#Collection-reset. Backbone#Collection-fetch
|
BackboneJS collection.reset() vs collection.fetch()
Date : March 29 2020, 07:55 AM
To fix the issue you can do reset sets the collection with an array of models that you specify: collection.reset( [ { name: "model1" }, { name: "model2" } ] );
collection.fetch( { url: someUrl, success: function(collection) {
// collection has values from someUrl
} } );
|
Backbonejs add to collection only if fetch data different
Date : March 29 2020, 07:55 AM
To fix the issue you can do I am writing a small app that calls a json request that has data about a track that is playing for a music player. Every 2-3 minutes that track on the json request changes. I am trying to get backbone to only fire and add another model to my collection if the previous entry called is different. Here is my code so far, however it keeps rendering the same data , Something like this should work startup: function() {
window.setInterval(function () {
console.log('fetching');
var track = new Track();
track.fetch()
.done(function() {
if(!this.isSamePrevTrack(track)) {
e.collection.add(track);
}
}.bind(this));
}.bind(this), 2000);
},
isSamePrevTrack: function(track) {
if(!this.prevTrack) {
this.prevTrack = track;
return false;
}
// Do some test to see if the track is the same
// ie. return this.prevTrack.get('id') === track.get('id');
});
|
BackboneJS fetch collection based on parameters
Date : March 29 2020, 07:55 AM
wish helps you Options are not stored by default, but you can override your initialize method to provide this functionality. You would then use this stored value in your parse method : Genres.Collection = Backbone.Collection.extend({
url: function() {
return 'path to my json';
},
initialize: function(opts) {
opts = opts || {};
this.genre = opts.genre || 'rock';
},
parse: function(response){
return response.data.genres[this.genre];
}
});
|
BackboneJS testing collection fetch
Date : March 29 2020, 07:55 AM
|