The Mobile Galaxy The New Mobile Era Has Come | Mobile – A Time To Remember

27Jul/120

Backbone model dynamic url method

-- Backbone Model /View -- when model changed, trigger view refresh ;

--Dynamic refresh a  grid/ table by different url;

Main.MappingsData = Backbone.Model.extend({
	defaults:{
		query:'',
		Mappings:null
	},
	url:"/dummy.json",
	
	  refetch: function(options){
		    _.bindAll(this, "refreshUserData");
		    console.log("refetch.....");
	        console.log(this);
	        this.fetch({success:this.refreshUserData, error:this.errorUserData});
	    },
	    
	  initialize: function(options){
		    _.bindAll(this, "refreshUserData");
		    console.log("Initialize.....");
	        console.log(this);
	        this.fetch({success:this.refreshUserData, error:this.errorUserData});
	    },
	  refreshUserData: function(data){
		    console.log("refreshUserData.....");
	        console.log(data);
	        console.log(this);
	    },

	    errorUserData: function(data){
	        alert("ERROR: Fetching user data");
	    }	 
	});
	
Main.HomeView = Backbone.View.extend({
  tagName: 'div',
  events: {
	    "click #loadmore": "loadMore"
	  },
	  
  initialize: function() {
    _.bindAll(this, 'render');
    this.model.bind('change', this.render);
  },

  render: function() {
    var compiledHome = Handlebars.compile($("#home-template").html());
    var sampleData =this.model.toJSON();
	console.log("sampleData...");
	console.log(sampleData);
    $(this.el).html(compiledHome(sampleData));
    return this;
  },
    loadMore:function(){
      	this.model.trigger('clear', this);
    	this.model.url="/dummy2.json";
         var self = this;
         this.model.fetch({
             success:function() {
            	 console.log(this.model.url);   
             }
         });
    }  
});

	var dummy= new Main.MappingsData();

    var view =new Main.HomeView({model:dummy}));

 

 

Share