CanJS添加自定义MODEL方法 [英] CanJS add custom MODEL method

查看:97
本文介绍了CanJS添加自定义MODEL方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想添加另一个函数以从CanJs模型中获取结果

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

我有这样的东西:

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

如果我将方法命名为findAll,它将正常工作,否则,将
命名为findNearNear它永远不会到达回调

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

我应该以某种方式扩展MODEL吗?

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

非常感谢您

推荐答案

CanJS仅将转换添加到标准 findOne findAll 等的Model实例中。模型方法。您必须通过 VideoMaker.model (对于单个项目)或 VideoMaker.models 运行结果,自己执行其他操作code>(用于多个项目):

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

这篇关于CanJS添加自定义MODEL方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆