CanJS add custom MODEL method

梦想与她 提交于 2019-12-08 08:32:15

问题


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


回答1:


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



回答2:


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
} )


来源:https://stackoverflow.com/questions/19877422/canjs-add-custom-model-method

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