【问题标题】:Dynamic properties for CanJS model?CanJS模型的动态属性?
【发布时间】:2012-09-16 13:14:41
【问题描述】:

我想在我的模型中添加不在 REST 服务结果中的动态属性。这些动态属性会缩短名称、格式化日期等。例如,我的 CanJS 模型是这样的:

var MyModel = can.Model({
    findAll: 'GET /Services/all'),
    findOne: 'GET /Services/{id}'),
    create:  'POST /Services/all'),
    update:  'PUT /Services/{id}'),
    destroy: 'DELETE /Services/{id}')
}, {});

然后我像这样检索数据:

MyModel.findAll({}, function(data) {
    $('#main').html(can.view('templates/List.ejs'), data));
});

这就是我的 List.ejs 模板的样子:

<% for(var i = 0; i < this.length; i++) { %>
    <div class="thumb"><img src="http://cdn.somewhere.com/images/logo.<%= this[i].BaseName %>.png" /></div>
    <div class="title"><%= this[i].Name %></div>
    <div class="date"><%= moment(this[i].StartDate).format('MMM DD, YYYY') %> - <%= moment(this[i].EndDate).format('MMM DD, YYYY') %></div>
    <div class="location"><%= this[i].LocationFriendly %></div>
    <div class="description"><%= this[i].Description %></div>
<% } %>

请注意我在模板中为图像 src 和开始/结束日期所做的逻辑。我想把这个逻辑放在模型中,所以我在模板中要做的就是:

<% for(var i = 0; i < this.length; i++) { %>
    <div class="thumb"><img src="<%= this[i].ImageURL %>" /></div>
    <div class="title"><%= this[i].Name %></div>
    <div class="date"><%= this[i].StartDateFriendly %> - <%= this[i].EndDateFriendly %></div>
    <div class="location"><%= this[i].LocationFriendly %></div>
    <div class="description"><%= this[i].Description %></div>
<% } %>

如何将此逻辑移动到模型层或比模板更好的位置?感谢您提供任何帮助或建议。

【问题讨论】:

    标签: javascript javascript-framework canjs canjs-model


    【解决方案1】:

    最简单的方法是在模型上创建函数:

    var MyModel = can.Model({
        findAll: 'GET /Services/all',
        findOne: 'GET /Services/{id}',
        create:  'POST /Services/all',
        update:  'PUT /Services/{id}',
        destroy: 'DELETE /Services/{id}'
    }, {
        imageUrl : function(){
            return "http://cdn.location.com/" + this.BaseName + ".png"
        },
        startDateFriendly : function(){
            return moment(this.StartDate).format('MMM DD, YYYY')
        },
        endDateFriendly : function(){
            return moment(this.StartDate).format('MMM DD, YYYY')
        },
        locationFriendly: function() {
            // ...
        }
    });
    

    然后你可以从视图中调用这些函数:

    <% for(var i = 0; i < this.length; i++) { %>
        <div class="thumb"><img src="<%= this[i].imageUrl() %>" /></div>
        <div class="title"><%= this[i].Name %></div>
        <div class="date"><%= this[i].startDateFriendly() %> - <%= this[i].endDateFriendly() %></div>
        <div class="location"><%= this[i].locationFriendly %></div>
        <div class="description"><%= this[i].Description %></div>
    <% } %>
    

    【讨论】:

    • 我刚试过这个,但是模板给出了一个错误:未捕获的类型错误:对象#没有方法'imageUrl'。我也试过不带括号,但在模板上显示为空白。你知道prototypeProperties辅助方法的EJS模板中是否有什么特别的事情要做吗?
    • 它在切换到真正的 REST 服务而不是模拟服务后工作。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2016-03-17
    • 2019-03-04
    • 1970-01-01
    • 2013-08-11
    • 1970-01-01
    • 2019-12-21
    • 1970-01-01
    • 2021-12-10
    相关资源
    最近更新 更多