Last active
May 24, 2018 18:19
-
-
Save ASH-terrance/d7ff927bbe7743dca9eb3d9d64b71ab5 to your computer and use it in GitHub Desktop.
Service Example
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
import Ember from 'ember'; | |
export default Ember.Component.extend({ | |
connected: Ember.inject.service('connected-api'), | |
didInsertElement() { | |
this._super(...arguments); | |
//this.gyms = this.get('connected').gymData() | |
} | |
}); |
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
import Ember from 'ember'; | |
export default Ember.Controller.extend({ | |
appName: 'Ember Twiddle', | |
/** | |
* This can be inject anywhere in the app. | |
*/ | |
connected: Ember.inject.service('connected-api'), | |
/** | |
* sudo code: calling a method on the service with then chaining | |
*/ | |
gymData() { | |
return this.get('connected').gymData().then((response) => { | |
return response | |
}). | |
catch((error) => { | |
return error.message | |
}) | |
}, | |
init() { | |
this._super(...arguments); | |
/* | |
* Can call method in service at any point. and set it in memeory | |
* if the service can return promises it gives the flexability of .then() chaining and catching errors. | |
* this data could be passed down to nested routes | |
*/ | |
this.gyms = this.get('connected').gymData() | |
this.oneGym = this.get('connected').myGymes() | |
/** | |
* this.gyms = gymData() | |
*/ | |
}, | |
}); |
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
import Ember from 'ember'; | |
export default Ember.Service.extend({ | |
/* | |
* could have multiple to make different calls if need (flexability) | |
*They are singleton’s (meaning there is only a single instance in your entire application), and they are only created when you get them (this.get('serviceName') ). They would only be destroyed when the whole application is destroyed. | |
*/ | |
gymData() { | |
return [ | |
{id: 1, name: '24 Hour Fitness'}, | |
{id: 2, name: 'LA Fitness'}, | |
{id: 3, name: 'Golds Gym'}, | |
] | |
}, | |
myGymes(user) { | |
return [{id: 1, name: '24 Hour Fitness'}] | |
}, | |
/** | |
pollGym: task(function * () { | |
while (true) { | |
yield timeout(10000); | |
let gym = yield this.gymData(); | |
return gym | |
} | |
}).restartable() | |
*/ | |
//etc ... | |
}); |
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
{ | |
"version": "0.13.1", | |
"EmberENV": { | |
"FEATURES": {} | |
}, | |
"options": { | |
"use_pods": false, | |
"enable-testing": false | |
}, | |
"dependencies": { | |
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.js", | |
"ember": "2.16.2", | |
"ember-template-compiler": "2.16.2", | |
"ember-testing": "2.16.2" | |
}, | |
"addons": { | |
"ember-data": "2.16.3" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment