logo
down
shadow

unable to call fetch on a backbone model


unable to call fetch on a backbone model

By : user2955262
Date : November 22 2020, 10:38 AM
Hope this helps You likely need to use bind to make sure this is the context of your View object.
As you mentioned in the comments, you can do:
code :
setInterval(this.jsonRequest.bind(this), 1000);


Share : facebook icon twitter icon
Backbone JS: Assign a model attribute in fetch call

Backbone JS: Assign a model attribute in fetch call


By : user2119507
Date : March 29 2020, 07:55 AM
To fix this issue Edit: without binding, the this pointer in the success callback points to the window object
code :
var form_model = Backbone.Model.extend({ 
urlRoot: QuoteForm.ajaxurl,
data: "",

initialize: function()
{
    this.fetch({
        data: { action: 'quote_form_data' },
        success: function (response) {
            // this bit won't assign
            this.data = response.toJSON();
        }.bind(this)
    });
}   

});
var form_model = Backbone.Model.extend({ 
urlRoot: QuoteForm.ajaxurl,
data: "",

initialize: function()
{
    this.fetch({
        data: { action: 'quote_form_data' },
        success: _.bind(function (response) {
            // this bit won't assign
            this.data = response.toJSON();
        }, this)
    });
}   

});
How to trigger an error during Backbone model fetch call?

How to trigger an error during Backbone model fetch call?


By : user2745232
Date : March 29 2020, 07:55 AM
will help you If you want to execute the callback attached to the error option, you'll need to modify your model's parse function as follows.
code :
parse: function(response, options) {
    var data = response.modeldata;
    var error = options.error;

    if (data.inaccessible === true) {
        if ( error ) error( this, response, options );
    } else {
        return data;
    }
}
Ajax in Backbone fetch finishes after fetch call exits, so no success/fail event in fetch call

Ajax in Backbone fetch finishes after fetch call exits, so no success/fail event in fetch call


By : silent boy
Date : March 29 2020, 07:55 AM
seems to work fine In my Backbone view initialize, I call an overridden fetch. Fetch checks if data is in localStorage, and if not, pulls data with ajax and populates into localStorage.
code :
fetch: function(options) {
  if(!localStorage.getItem("image")) {
      var self = this;
      $.ajax({
        url: '[url]'  //Could add async but how to trigger render from this function?
      }).done(function(response) {            
        console.log("Fetched"); 
        $.each(response.items, function(i, item) {
          self.create(item);  // saves model to local storage
        });
        options.success(self); //call success func
        self.trigger('sync', self, response, options); //fire sync event
      }).fail(function(response) {
         console.log("Failure in fetch");
         options.error(responce) //call error func
      });
  } else {
      // fetch from localStorage works fine
      return Backbone.Collection.prototype.fetch.call(this, options);
  }
}
initialize: function() {
  var imageList = new ImageList(),
      simageel = $(this.el).find('#imagec'),
      simageView;
  this.listenToOnce(imageList,"sync",function(){
    console.log("call next method what you want to do after fetching's finished");
  });
  imageList.fetch({
    error: function() { console.log(arguments); },
    success: function(collection) {
      collection.each(function (model) {
        simageView = new SImageView({
          model: model
        });
        simageel.html(simageView.render().el);
        console.log('Fetch success.');  // Never fires
      });
    }
  });
Can't call fetch directly in Backbone model listenTo

Can't call fetch directly in Backbone model listenTo


By : Ataberk
Date : March 29 2020, 07:55 AM
With these it helps The handler for each type of event is passed a certain set of arguments. The Catalog of Events has this to say about a "change" event:
"change" (model, options) — when a model's attributes have changed.
code :
this.listenTo(members, 'change', this.fetch)
fetch(the_model_that_changed, some_options_object)
How to skip parse function call in backbone model while calling fetch

How to skip parse function call in backbone model while calling fetch


By : Benjamin
Date : March 29 2020, 07:55 AM
fixed the issue. Will look into that further Put the skip condition in your parse function. How you determine the skip condition is up to you.
code :
parse: function(response) {
      if(skipParse)
          return response;
      //parse the response here. If the code reaches this point, 
      //it means you want to parse it.
      return response;
   },
shadow
Privacy Policy - Terms - Contact Us © ourworld-yourmove.org