Last active
August 29, 2015 14:02
-
-
Save rcstr/93ff251231fdfdbf9d23 to your computer and use it in GitHub Desktop.
version with promise
This file contains hidden or 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
AcornsTest.StockRoute = Ember.Route.extend({ | |
model: function(params) { | |
"use strict"; | |
var url_params = params.slug.split('|'), | |
url = AcornsTest.Config.quandl.URL + '/' + url_params[0] + '/' + url_params[1] + '.json', | |
stockInStore = this.store.getById('stock', url_params[1]), | |
promise = Ember.Deferred.create(), | |
today = new Date(), | |
yearAgo = new Date(), | |
self = this; | |
yearAgo.setFullYear(today.getFullYear() - 1); | |
today = today.getFullYear()+'-'+today.getMonth()+'-'+today.getDate(); | |
yearAgo = yearAgo.getFullYear()+'-'+yearAgo.getMonth()+'-'+yearAgo.getDate(); | |
if(stockInStore && stockInStore.get('data').length) { | |
promise.resolve(stockInStore); | |
} | |
Ember.$.getJSON(url,{ trim_start: yearAgo, trim_end: today, auth_token: AcornsTest.Config.quandl.APIKEY }) | |
.then(function(data) { | |
if(stockInStore) { | |
promise.resolve(stockInStore.set('data', data.data)); | |
} else { | |
promise.resolve(self.store.createRecord('stock', { | |
id: data.code, | |
source_code: data.source_code, | |
code: data.code, | |
name: data.name, | |
description: data.description, | |
display_url: data.display_url, | |
source_name: data.source_name, | |
data: data.data, | |
slug: data.source_code+'|'+data.code | |
})); | |
} | |
}); | |
return promise; | |
} | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment