CanJS add custom MODEL method

2019-06-06 02:30发布

I want to add another function to get result from a CanJs Model

i Have something like this:

   VideomakerModel = can.Model({
        id:'ID',
        findAll: 'GET /videomakers/',
        findNear:  function( params ){
                         return $.post("/get_near_videomakers/",
                         {address:params.address},
                         undefined ,"json")
                    }
         },{});

    VideomakerModel.findNear({address : "Milan"}, function(videomakers) {
                var myList = new VideomakerControl($('#accordionVm'), {
                videomakers : videomakers,
                view: 'videomakersList'
            });
        });

If I name the method findAll it works correctly, otherwise naming it findNear it never reach the callback

should I extend MODEL is some way?? is there a way of add a function like FindAll?

thank you very much

2条回答
聊天终结者
2楼-- · 2019-06-06 02:36

CanJS only adds the conversion into a Model instance for the standard findOne, findAll etc. Model methods. You will have to do that yourself in your additional implementation by running the result through VideoMaker.model (for a single item) or VideoMaker.models (for multiple items):

VideomakerModel = can.Model({
  id:'ID',
  findAll: 'GET /videomakers/',
  findNear:  function( params ) {
    var self = this;
    return $.post("/get_near_videomakers/", {
      address:params.address
    }, undefined ,"json").then(function(data) {
      return self.model(data);
    });
  }
 },{});
查看更多
手持菜刀,她持情操
3楼-- · 2019-06-06 02:45

If I understand the question, it is necessary to do so:

 VideomakerModel = can.Model({
       id:'ID',
       findAll: 'GET /videomakers/'
    }, 
    {
       findNear: function(options, success){
           can.ajax({
                url: "/get_near_videomakers/",
                type: 'POST',
                data: options,
                success: success
            })
       }  
    })

var myList = new VideomakerControl({});

myList.findNear({address:params.address}, function(resulrRequest){
    //success
} )
查看更多
登录 后发表回答