Last active
January 30, 2016 10:34
-
-
Save trabus/94a6bebb7a38bf01ca4c to your computer and use it in GitHub Desktop.
query-param-service
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
import Ember from 'ember'; | |
const {computed} = Ember; | |
export default Ember.Service.extend({ | |
_account: null, | |
account: computed('_account', { | |
get(key) { | |
return this.get('_account'); | |
}, | |
set(key, val) { | |
this.updateAccountValue(val); | |
return this.get('_account'); | |
} | |
}), | |
isDirty: false, | |
isValidAccount(val) { | |
return val > 3; | |
}, | |
validateValue(val) { | |
if (this.isValidAccount(val)) { | |
return val; | |
} else { | |
return this.get('_account'); | |
} | |
}, | |
initAccountValue(val) { | |
this.set('_account', this.validateValue(val)); | |
}, | |
updateAccountValue(val) { | |
let valid = this.isValidAccount(val); | |
if (valid) { | |
if (this.get('_account') === null) { | |
this.initAccountValue(val); | |
} else { | |
this.set('_account', val); | |
} | |
} | |
return valid; | |
}, | |
checkDirty(val) { | |
if (this.get('isDirty')) { | |
val = this.get('account'); | |
this.set('isDirty', false); | |
} | |
return val; | |
}, | |
changeAccount(val){ | |
let value = this.get('account'); | |
if (typeof val === 'string'){ | |
if (val === 'prev') { | |
value--; | |
} else if (val === 'next') { | |
value++; | |
} else { | |
value = parseInt(val); | |
} | |
} | |
this.set('isDirty', true); | |
this.set('account', this.validateValue(value)); | |
} | |
}); |
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
import Ember from 'ember'; | |
const {computed, inject} = Ember; | |
export default Ember.Controller.extend({ | |
as: inject.service('account'), | |
appName:'Ember Twiddle', | |
queryParams: ['account'], | |
account:null, | |
inputValue: computed.alias('account'), | |
// this computed is key, as it watches both the query param and the value on the service | |
isValidAccount: computed('account', 'as.account', { | |
get() { | |
// if the service value is dirty, use it | |
let val = this.get('as').checkDirty(this.get('account')); | |
// set the new val | |
this.set('account', val); | |
return this.get('as').updateAccountValue(val); | |
} | |
}), | |
actions: { | |
changeAccount(val) { | |
this.get('as').changeAccount(val); | |
} | |
} | |
}); |
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
import Ember from 'ember'; | |
export default Ember.Component.extend({ | |
}); |
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
{ | |
"version": "0.5.0", | |
"EmberENV": { | |
"FEATURES": {} | |
}, | |
"options": { | |
"enable-testing": false | |
}, | |
"dependencies": { | |
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.js", | |
"ember": "https://cdnjs.cloudflare.com/ajax/libs/ember.js/2.2.0/ember.debug.js", | |
"ember-data": "https://cdnjs.cloudflare.com/ajax/libs/ember-data.js/2.2.0/ember-data.js", | |
"ember-template-compiler": "https://cdnjs.cloudflare.com/ajax/libs/ember.js/2.2.0/ember-template-compiler.js" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment