English 中文(简体)
Backbone.js: 铁路改道
原标题:Backbone.js: Change datetime field from Rails

When I print the created_at field of a model using Backbone.js( <i> {{created_at}} </i> ), I m getting: 2011-08-07T12:03:00Z.

I m试图将日期改为可读格式,而Im无法阅读。

我希望在印刷前删除T和Z。

感谢

问题回答

改变贵模板中的变量,或增加新的变量。 我要补充一个新的内容。

如果你行使这一职能,可以改为:

render: function() {
  var context = this.model.toJSON();
  context[ created_at_formatted ] = this.formatDate(context[ created_at ]);
  var html = this.template.to_html(context);
  $(this.el).html(html);
}

加上以下观点:

formatDate: function(d) {
  var pad = function (n) {
    return n < 10 ?  0  + n : n;
  };

  // in case the date is a string; you can remove this if you know it will be a date
  if (typeof date ===  string ) {
    d = new Date(d);
  }

  return [
    d.getUTCFullYear(),  - ,
    pad(d.getUTCMonth() + 1),  - ,
    pad(d.getUTCDate()),    ,
    pad(d.getUTCHours()),  : ,
    pad(d.getUTCMinutes()),  : ,
    pad(d.getUTCSeconds())
  ].join("");
}

Then the template would be like this:

<i> {{created_at_formatted}} </i>

为了简化日期格式功能,你还可以使用日期格式图书馆,如。 你还可以将日期格式移至模式。

Readable? Why not jQuery timeago? jQuery timeago

阁下,

toTemplateData: -> 
  _.extend
    created_duration: do => $.timeago @get("created_at") if @get("created_at")

阁下,

render: ->
  @$el.html(@template(@model.toTemplateData() ))

在您的模板中,

<time title="{{created_duration}}"></time>

You can use the strftime on your created_at

<i> {{created_at.strftime( %Y/%m/%d )}} </i>

本答复是正确的,但该日期的编排格式实际上应当是一个模式,因为它是业务逻辑。

我的改动是:

render: function() {
  var context = this.model.format().toJSON();
  var html = this.template.to_html(context);
  $(this.el).html(html);
}

以及你的模式,例如:

format() {
  this.set( created_at_formatted , formatDate(this.get( created_at )));
  return this;
}

formatDate: function(d) {
  // format date stuff (See Ben`s answer)
  return d;
}

And your template would render:

<i> {{created_at}} </i>

这消除了某些 red余,促进了再利用,更容易阅读。 也许我的答案需要一点工作,但希望大家能了解这个总的想法。





相关问题
rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

SSL slowness in EC2

We ve deployed our rails app to EC2. In our setup, we have two proxies on small instances behind round-robin DNS. These run nginx load balancers for a dynamically growing and shrinking farm of web ...

Auth-code with A-Za-z0-9 to use in an URL parameter

As part of a web application I need an auth-code to pass as a URL parameter. I am currently using (in Rails) : Digest::SHA1.hexdigest((object_id + rand(255)).to_s) Which provides long strings like : ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

activerecord has_many :through find with one sql call

I have a these 3 models: class User < ActiveRecord::Base has_many :permissions, :dependent => :destroy has_many :roles, :through => :permissions end class Permission < ActiveRecord::...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签