How to use Defer in canjs

偶尔善良 提交于 2020-01-06 15:18:39

问题


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'})
  }
},{})


来源:https://stackoverflow.com/questions/20996824/how-to-use-defer-in-canjs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!