requirejs - Backbone.js / require.js - Override model function to work with backend as a service -


good morning guys. have little understanding problem backbone.js. have javascript sdk backend service getter , setter methods datas platform.

i have load javascript sdk require.js it´s work fine. need create models work getter , setter methods data collection view. not have clue...maybe have right idea me.

this current model:

define(['jquery','underscore','backbone'], function($,_,backbone) {     var holidayperson = backbone.model.extend({          initialize: function() {              console.log("init model holidayperson");              this.on("change", function(data) {                console.log("change model holidayperson"+json.stringify(data));             });          }     });      return holidayperson; }); 

actually create instance of model in view:

define(['jquery','underscore','backbone','text!tpl/dashboard.html','holidayperson','apio'], function($,_,backbone,tpl, holidayperson, apio) {      template = _.template(tpl);     var usermodel = new holidayperson();      var dashboardview = backbone.view.extend({          id: 'givenname',          initialize: function() {             console.log("dashboard view load");             usermodel.on('change', this.render);              var user = new apio.user();             user.setusername('xxx');             user.setpassword('xxx');              apio.datastore.configurewithcredentials(user);              apio.employee.getemployees("firstname \"jon\" , lastname \"doe\"", {                 onok: function (objects) {                      console.log("apio: " + json.stringify(objects));                      usermodel.set({mail: objects[0]['data']['mail'],lastname: objects[0]['data']['lastname'], username: objects[0]['data']['username'], superior: objects[0]['data']['superior']});                  }             });         },          render: function() {             console.log("render dashboard view");             console.log(usermodel.get('mail'));             console.log(usermodel.get('lastname'));             this.$el.html(template());             return this;         }     });      return dashboardview; }); 

i think not right way...can override getter , setter method model ? or maybe url function ? best practice ?

thanks lot :-)

first of all, make sure render operation asynchronous, api call , usermodel params won't set until operation completes. if render method fires before that, render empty usermodel, since data not there yet.

second, model need not fetch own data, in opinion. if going have multiple users, use collection hold users , override collection's sync method handle fetching of data api, if there's no collection, seems logical me have method data fetching , setting thereafter, you've done.


Comments

Popular posts from this blog

java - Run spring boot application error: Cannot instantiate interface org.springframework.context.ApplicationListener -

reactjs - React router and this.props.children - how to pass state to this.props.children -

Excel VBA "Microsoft Windows Common Controls 6.0 (SP6)" Location Changes -