-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
34 changed files
with
421 additions
and
181 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
...book/user_interface_and_interaction/displaying_relative_dates_with_moment_js.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
## Problem | ||
|
||
You want to display dates in relative form. | ||
|
||
## Solution | ||
|
||
[Moment.js](http://momentjs.com) will be used for formatting the dates. | ||
|
||
Start by creating a new view, which would just render the result of | ||
`moment().fromNow()` and register a helper for it. | ||
|
||
```javascript | ||
App.FromNowView = Ember.View.extend({ | ||
tagName: 'time', | ||
template: Ember.Handlebars.compile('{{view.fromNow}}'), | ||
|
||
fromNow: function() { | ||
return moment(this.get('value')).fromNow(); | ||
}.property('value') | ||
}); | ||
|
||
Ember.Handlebars.helper('fromNow', App.FromNowView); | ||
``` | ||
|
||
Relative dates, however, need to be updated periodically, so we'll | ||
trigger a view re-render every second. | ||
|
||
```javascript | ||
App.FromNowView = Ember.View.extend({ | ||
// ... | ||
nextTick: null, | ||
|
||
didInsertElement: function() { | ||
this.tick(); | ||
}, | ||
|
||
tick: function() { | ||
var nextTick = Ember.run.later(this, function() { | ||
this.notifyPropertyChange('value'); | ||
this.tick(); | ||
}, 1000); | ||
this.set('nextTick', nextTick); | ||
}, | ||
|
||
willDestroyElement: function() { | ||
Ember.run.cancel(this.get('nextTick')); | ||
} | ||
}); | ||
``` | ||
|
||
Now the template can be like this: | ||
|
||
```html | ||
created {{fromNow value=createdAt}} | ||
``` | ||
|
||
## Discussion | ||
|
||
#### Example | ||
|
||
<a class="jsbin-embed" href="http://jsbin.com/uLETuPe/1/embed?live">JS Bin</a><script src="http://static.jsbin.com/js/embed.js"></script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.