如何申请timeago或 easydate在 <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;
}
关于javascript - 如何在 Backbone/Underscore 上应用 jQuery timeago 或 easydate?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11147773/