-
-
Notifications
You must be signed in to change notification settings - Fork 122
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from jasonmit/computed
[Enhancement] Adding moment computed fixes #6
- Loading branch information
Showing
13 changed files
with
261 additions
and
26 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import moment from './computeds/moment'; | ||
import ago from './computeds/ago'; | ||
|
||
export { | ||
moment, | ||
ago | ||
}; |
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,30 @@ | ||
import Ember from 'ember'; | ||
import moment from 'moment'; | ||
import { descriptorFor } from './moment'; | ||
|
||
var get = Ember.get; | ||
var emberComputed = Ember.computed; | ||
|
||
export default function computedAgo(date, maybeInputFormat) { | ||
var args = [date]; | ||
var momentArgs, computed, desc, input; | ||
|
||
computed = emberComputed(date, function () { | ||
momentArgs = [get(this, date)]; | ||
|
||
if (arguments.length > 1) { | ||
desc = descriptorFor.call(this, maybeInputFormat); | ||
input = desc ? get(this, maybeInputFormat) : maybeInputFormat; | ||
|
||
if (desc && computed._dependentKeys.indexOf(maybeInputFormat) === -1) { | ||
computed.property(maybeInputFormat); | ||
} | ||
|
||
momentArgs.push(input); | ||
} | ||
|
||
return moment.apply(this, momentArgs).fromNow(); | ||
}); | ||
|
||
return computed.property.apply(computed, args).readOnly(); | ||
} |
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,48 @@ | ||
import Ember from 'ember'; | ||
import moment from 'moment'; | ||
|
||
var get = Ember.get; | ||
var emberComputed = Ember.computed; | ||
var EnumerableUtils = Ember.EnumerableUtils; | ||
var a_slice = Array.prototype.slice; | ||
|
||
export function descriptorFor(propertyName) { | ||
var meta = Ember.meta(this); | ||
|
||
if (meta && meta.descs) { | ||
return meta.descs[propertyName]; | ||
} | ||
} | ||
|
||
export default function computedMoment(date, outputFormat, maybeInputFormat) { | ||
Ember.assert('More than one argument passed into moment computed', arguments.length > 1); | ||
|
||
var args = a_slice.call(arguments); | ||
var computed, self, momentArgs, desc; | ||
|
||
args.shift(); | ||
|
||
return computed = emberComputed(date, function () { | ||
self = this; | ||
momentArgs = [get(this, date)]; | ||
|
||
var propertyValues = EnumerableUtils.map(args, function (arg) { | ||
desc = descriptorFor.call(self, arg); | ||
|
||
if (desc && computed._dependentKeys.indexOf(arg) === -1) { | ||
computed.property(arg); | ||
} | ||
|
||
return desc ? get(self, arg) : arg; | ||
}); | ||
|
||
outputFormat = propertyValues[0]; | ||
|
||
if (propertyValues.length > 1) { | ||
maybeInputFormat = propertyValues[1]; | ||
momentArgs.push(maybeInputFormat); | ||
} | ||
|
||
return moment.apply(this, momentArgs).format(outputFormat); | ||
}).readOnly(); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import Ember from 'ember'; | ||
import { moment, ago } from 'ember-moment/computed'; | ||
|
||
export default Ember.Controller.extend({ | ||
now: new Date(), | ||
lastHour: new Date(new Date().valueOf() - (60*60*1000)), | ||
date: new Date(), | ||
computedDate: moment('date', 'MM/DD/YY hh:mm:ss'), | ||
computedOneHourAgo: ago('lastHour') | ||
}); |
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 |
---|---|---|
@@ -1,10 +1,13 @@ | ||
import Ember from 'ember'; | ||
|
||
export default Ember.Route.extend({ | ||
model: function() { | ||
return { | ||
now: new Date(), | ||
lastHour: new Date(new Date().valueOf() - (60*60*1000)) | ||
}; | ||
setupController: function (controller, model) { | ||
this._super(controller, model); | ||
|
||
setInterval(function () { | ||
Ember.run(function () { | ||
controller.set('date', new Date()); | ||
}); | ||
}, 1000); | ||
} | ||
}); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import Ember from 'ember'; | ||
import date from '../helpers/date'; | ||
import moment from 'moment'; | ||
import { ago as computedAgo } from 'ember-moment/computed'; | ||
|
||
module('momentComputed'); | ||
|
||
var createSubject = function (attrs) { | ||
return Ember.Object.extend(Ember.$.extend({ | ||
date: new Date(new Date().valueOf() - (60*60*1000)), | ||
ago: computedAgo('date') | ||
}, attrs || {})).create(); | ||
}; | ||
|
||
test('Formatter - get', function() { | ||
var subject = createSubject(); | ||
equal(subject.get('ago'), 'an hour ago'); | ||
}); | ||
|
||
test('Formatter - get #2', function() { | ||
var subject = createSubject(); | ||
equal(subject.get('ago'), 'an hour ago'); | ||
subject.set('date', new Date(new Date().valueOf() - (60*60*2000))); | ||
equal(subject.get('ago'), '2 hours ago'); | ||
}); |
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,77 @@ | ||
import Ember from 'ember'; | ||
import date from '../helpers/date'; | ||
import { moment as computedMoment } from 'ember-moment/computed'; | ||
|
||
module('momentComputed'); | ||
|
||
var alias = Ember.computed.alias; | ||
|
||
var createSubject = function (attrs) { | ||
return Ember.Object.extend(Ember.$.extend({ | ||
date: date(0), | ||
shortDate: computedMoment('date', 'MM/DD') | ||
}, attrs || {})).create(); | ||
}; | ||
|
||
test('Formatter - get', function() { | ||
var subject = createSubject(); | ||
equal(subject.get('shortDate'), '12/31'); | ||
}); | ||
|
||
test('Date - set', function() { | ||
var subject = createSubject(); | ||
subject.set('date', date('2013-02-08T09:30:26')); | ||
equal(subject.get('shortDate'), '02/08'); | ||
}); | ||
|
||
test('Formatter - invalid date', function() { | ||
var subject = createSubject({ date: 'ZZZZZ' }); | ||
equal(subject.get('shortDate'), 'Invalid date'); | ||
}); | ||
|
||
test('Formatter - is computed handled', function() { | ||
var subject = createSubject({ | ||
_format: 'MM/DD', | ||
format: alias('_format'), | ||
shortDate: computedMoment('date', 'format') | ||
}); | ||
|
||
equal(subject.get('shortDate'), '12/31'); | ||
subject.set('_format', 'MM'); | ||
equal(subject.get('shortDate'), '12'); | ||
}); | ||
|
||
test('Observers trigger on date change', function() { | ||
var observeFired = false; | ||
|
||
var subject = createSubject({ | ||
_format: 'MM/DD', | ||
format: alias('_format'), | ||
shortDate: computedMoment('date', 'format'), | ||
shortDateChanged: function () { | ||
observeFired = true; | ||
}.observes('shortDate') | ||
}); | ||
|
||
equal(subject.get('shortDate'), '12/31'); | ||
subject.set('_format', 'MM'); | ||
equal(observeFired, true); | ||
}); | ||
|
||
test('Observers trigger on date change', function() { | ||
var observeFired = false; | ||
|
||
var subject = createSubject({ | ||
shortDateChanged: function () { | ||
observeFired = true; | ||
}.observes('shortDate') | ||
}); | ||
|
||
equal(subject.get('shortDate'), '12/31'); | ||
|
||
subject.set('date', date('2013-02-08T09:30:26')); | ||
|
||
equal(subject.get('shortDate'), '02/08'); | ||
|
||
equal(observeFired, true); | ||
}); |
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