问题
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