Last active
January 30, 2019 20:55
-
-
Save GCheung55/61fa44ae5d57211e6fadd014d901ab35 to your computer and use it in GitHub Desktop.
Demonstrate nested routes with model-refreshing query params
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 Controller from '@ember/controller'; | |
export default Controller.extend({ | |
appName: 'Ember Twiddle', | |
queryParams: [ | |
// The query param `user-id` will update the computed property `userId`. | |
{ 'user-id': 'userId' } | |
], | |
// Default value if query param is empty. | |
userId: '', | |
actions: { | |
changeUser(userId) { | |
// Updating the query-param `user-id`, which will automatically propogate the change of `userId` and refresh the model. | |
this.set('user-id', userId); | |
} | |
} | |
}); |
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 Controller, { inject as controller } from '@ember/controller'; | |
import { readOnly } from '@ember/object/computed'; | |
export default Controller.extend({ | |
// Inject a controller - must be a parent. Cannot be a sibling or child. | |
application: controller(), | |
// Demonstrate a computed property that reads the value from the application controller. | |
applicationCallCount: readOnly('application.model.count') | |
}); |
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 EmberRouter from '@ember/routing/router'; | |
import config from './config/environment'; | |
const Router = EmberRouter.extend({ | |
location: 'none', | |
rootURL: config.rootURL | |
}); | |
Router.map(function() { | |
this.route('memberships'); | |
}); | |
export default Router; |
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 Route from '@ember/routing/route'; | |
export default Route.extend({ | |
// Query param configuration - here, changing user-id will refresh the model | |
queryParams: { | |
'user-id': { | |
refreshModel: true | |
} | |
}, | |
count: 0, | |
model(params) { | |
// To demonstrate that the mode has refreshed, update and pass the a count to the controller | |
this.incrementProperty('count'); | |
return { | |
...params, | |
count: this.count | |
} | |
} | |
}); |
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 Route from '@ember/routing/route'; | |
export default Route.extend({ | |
// Query param configuration - here, changing user-id will refresh the model | |
queryParams: { | |
'user-id': { | |
refreshModel: true | |
} | |
}, | |
beforeModel() { | |
console.log('beforeModel triggered'); | |
}, | |
model() { | |
return { | |
indexRandom: Math.random() * 100 | |
} | |
} | |
}); |
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 Route from '@ember/routing/route'; | |
export default Route.extend({ | |
// Query param configuration - here, changing user-id will refresh the model | |
queryParams: { | |
'user-id': { | |
refreshModel: true | |
} | |
}, | |
model() { | |
return { | |
membershipsRandom: Math.random() * 100 | |
} | |
} | |
}); |
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.15.1", | |
"EmberENV": { | |
"FEATURES": {} | |
}, | |
"options": { | |
"use_pods": false, | |
"enable-testing": false | |
}, | |
"dependencies": { | |
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.js", | |
"ember": "3.4.3", | |
"ember-template-compiler": "3.4.3", | |
"ember-testing": "3.4.3" | |
}, | |
"addons": { | |
"ember-data": "3.4.2" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment