javascript - 如何在 Backbone/Underscore 上应用 jQuery timeago 或 easydate?

标签 javascript jquery backbone.js underscore.js template-engine

如何申请timeagoeasydate<time class="prettydate"><%= created_at %></time>" (输出像这样的 Mon Jun 18 03:46:27 +0000 2012 )?

我尝试了下面的方法,但它不起作用,因为我正在使用 Backbone/Underscore 来呈现日期。

$(function() {
  $('time.prettydate').easydate();
});

这是我的看法:

( function ( views ){

    views.ResultView = Backbone.View.extend({
        tagName : 'li',
        template: _.template($('#resultItemTemplate').html()),

        initialize: function() {
            this.model.bind('change', this.render, this);
            this.model.bind('destroy', this.remove, this);
        },

        render : function () {
            //this.$el.html(this.template(this.model.toJSON()));
            var html = this.template(this.model.toJSON());
            this.$el.html($(html).timeago());
        }
    });

})( app.views );

最佳答案

大概你在 View 中有这样的东西:

render: function() {
    var html = _.template(...);
    this.$el.append(html);
    return this;
}

您需要做的就是在附加插件时将插件直接应用于 html:

render: function() {
    var html = _.template(...);
    this.$el.append($(html).timeago());
    return this;
}

演示:http://jsfiddle.net/ambiguous/Fk6xF/

如果您正在执行 this.$el.html(...),这同样有效。

如果您需要应用 timeago 的片段您的模板中,那么您将不得不寻找它们并将 .timeago() 应用于单独的片段.像这样的东西应该可以解决问题:

render: function() {
    var html = _.template(...);
    this.$el.html(html);
    this.$el.find('.timeago').timeago(); // Assuming you're using class="timeago"
    return this;
}

演示:http://jsfiddle.net/ambiguous/dHr6D/

关于javascript - 如何在 Backbone/Underscore 上应用 jQuery timeago 或 easydate?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11147773/

相关文章:

javascript - 如何从 View 重新加载部分 View ?

javascript - Chart.js : How to fill the area under the line to a certain point?

javascript - 将日期字符串与 jquery 中的日期对象进行比较

backbone.js - 模式的 Backbone 路由器

javascript - 点击事件未在 Firefox 中触发 - 主干

Backbone.js "create"回调?

javascript - 按第二个值对二维数组进行排序

javascript - 这在 JavaScript : array[n] = true; 中有什么作用

javascript - 获取页面的 html 源代码,包括由 Jquery 或 Javascript 动态创建的元素

javascript - PHP 中的 HTML 数组字段解析