骨干网模型 - 扩展API调用 [英] Backbone model - extending api calls

查看:94
本文介绍了骨干网模型 - 扩展API调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的工作我的第一次的NodeJS /骨干工程。我需要做的是用另外的方法来扩展用户模型API。由于REST采用通用的交/获得/ PUT请求,你怎么扩大与其他API调用(即阻止用户帐户,我不想更新用户做出/用户/关闭URL)骨干模式?

我可以把丑的路线,但我找专业人士从正确的方式。

我的骨干模型

 定义([jQuery的,脊梁]
功能($,骨干){
    VAR用户= Backbone.Model.extend({
        urlRoot:'/用户,        默认值:{
            用户名: '',
            密码:'',
            电子邮件:''
        }
    });
    返回用户;
}
);

我的NodeJS路由器

  app.put('/用户,userController.register);
    app.post('/用户,userController.update);
    app.get('/用户,userController.list);
    app.delete('/用户,userController.delete);


解决方案

为什么不加挡和关闭属性模型,然后用标准的REST / CRUD API调用。

然后你的行动阻止和取消也只是标准模型的更新,以逻辑来处理你的API方法阻止,主动型状态。

 定义([jQuery的,脊梁]
功能($,骨干){
    VAR用户= Backbone.Model.extend({
        urlRoot:'/用户,        默认值:{
            用户名: '',
            密码:'',
            电子邮件:'',
            阻止:假的,
            主动:真
        },        块:功能(){
            this.set('受阻',真);
            this.save();
        },        关闭:函数(){
            this.set(激活,假);
            this.save();
        },    });
    返回用户;
}
);

修改 - 根据您的意见,需要更新区分

如果您需要在服务器上更新领域区分,以便运行路线特定的验证为例。那么你可能会需要调用自定义路线的每个具体行动。这样做的一个巧妙的办法是在通话过程中覆盖URL保存模型。

这是更新的典范。

 定义([jQuery的,脊梁]
功能($,骨干){
    VAR用户= Backbone.Model.extend({
        urlRoot:'/用户,        默认值:{
            用户名: '',
            密码:'',
            电子邮件:'',
            阻止:假的,
            主动:真
        },        块:功能(){
            this.set('受阻',真);
            this.save(这一点,网址:{url:'/用户/块'});
        },        关闭:函数(){
            this.set(激活,假);
            this.save(这一点,网址:{url:'/用户/禁用'});
        },    });
    返回用户;
}
);

然后,你将有以下途径

  app.put('/用户,userController.register);
app.post('/用户,userController.update);
app.get('/用户,userController.list);
app.delete('/用户,userController.delete);
app.post('/用户/块',userController.block);
app.post('/用户/停用,userController.deactivate);

I'm working on my first nodejs/backbone project. What I need to do is to extend user model api with additional methods. As REST uses universal post/get/put request, how do you extended backbone model with other api calls (ie. block user account where I don't want to update user and make /user/deactivate url)?

I can take ugly routes, but I looking for the "right way" from pros.

My backbone model

define(["jquery", "backbone"],
function($, Backbone) {
    var User = Backbone.Model.extend({
        urlRoot: '/user',

        defaults: {
            username: '',
            password: '',
            email: ''
        }
    });
    return User;
}
);

My nodejs "router"

    app.put('/user', userController.register);
    app.post('/user', userController.update);
    app.get('/user', userController.list);
    app.delete('/user', userController.delete);

解决方案

Why not add block and deactivate attributes to your model, then just use standard REST / CRUD API calls.

Then your actions block and deactivate would just be standard model updates, with logic to handle blocked and active model states in your API methods.

define(["jquery", "backbone"],
function($, Backbone) {
    var User = Backbone.Model.extend({
        urlRoot: '/user',

        defaults: {
            username: '',
            password: '',
            email: '',
            blocked: false,
            active: true
        },

        block: function () {
            this.set('blocked', true);
            this.save();
        },

        deactivate: function () {
            this.set('active', false);
            this.save();
        },

    });
    return User;
}
);

EDIT - Based on your comment, the need to distinguish between updates

If you need to distinguish between field updates on the server, in order to run route specific validation for example. Then you're probably going to need to call custom routes for each specific action. A neat way of doing this would be to override the url during the call to save the model.

An updated model example.

define(["jquery", "backbone"],
function($, Backbone) {
    var User = Backbone.Model.extend({
        urlRoot: '/user',

        defaults: {
            username: '',
            password: '',
            email: '',
            blocked: false,
            active: true
        },

        block: function () {
            this.set('blocked', true);
            this.save(this, { url: '/user/block' });
        },

        deactivate: function () {
            this.set('active', false);
            this.save(this, { url: '/user/deactivate' });
        },

    });
    return User;
}
);

Then you would have the following routes

app.put('/user', userController.register);
app.post('/user', userController.update);
app.get('/user', userController.list);
app.delete('/user', userController.delete);
app.post('/user/block', userController.block);
app.post('/user/deactivate', userController.deactivate);

这篇关于骨干网模型 - 扩展API调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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