How to use Defer in canjs

2019-08-31 07:05发布

问题:

Featoff = can.Model.extend("featoff",{
        findAll: {
        url: '/api.php/specialoffers?',
        type: 'GET',
        data: { max : 10 , pid : 977 , sid : 5934 }
        }
      }, {});

Feat = can.Control({ init: function()
 { var that = this; can.view('images/js/mn/temps/featured.ejs', featoff.findAll({}).then(function(d) 
{ return { offerdata : d, secTitle : that.element.data('title') }; })).done(function(frag) 
{ that.element.html(frag); }) }}); 

I am calling this using new Feat();

this is working now. So now I wanna reuse the same Feat control with different set of parameters in the findAll, How can i do so? What method to use and how to use?? also can I also defer or override the can.view for the same ??

Can I also have a single Base controller and just keep overriding the parameters?

回答1:

To override the view you can extend your Feat object FeatExtended = Feat.extend{ and use a different view in the init method.

You can also give parameters when calling new Feat({view : MY_VIEW}). An options object is passed to init() as describe here.

For you findAll question, you should define findAll as a method as describe in the documentation

Hope that helps !

EDIT :

For example : you can pass a params object defining an option to be added in URL and the method of the HTTP call

Featoff = can.Model.extend({
  findAll : function(params){
    return $.ajax({
      url: '/api.php/specialoffers?' + params.myoption,
      type: params.method,
      dataType: 'json'})
  }
},{})