컨νΈλ‘€λ¬κ° λ€μκ³Ό κ°μ΄ μ μ λ κ²½μ°
App.IndexController = Ember.Controller.extend({
queryParams: ['foo'],
foo: ''
});
Routes setupControllerμμ λ€μκ³Ό κ°μ μμ μ μνν©λλ€.
setupController: function(controller, model){
controller.set('model', model);
controller.set('foo', 'bar'); // <-- setting this should update the query param
}
쿼리 λ§€κ° λ³μκ° URLμ μ€μ λμ§ μμ΅λλ€.
μ¬κΈ°μμ μμ λΉμ λ³Ό μ μμ΅λλ€ : http://emberjs.jsbin.com/dekuj/1/edit
Ember.run.later
μμ λμμ΅λλ€ (λ¨Όμ μμ ν ꡬ체νλμλμ§ νμΈ) : http://emberjs.jsbin.com/kowoqo/1/edit
@machty- μ¬κΈ°μ μμλλ λμμ 무μμ λκΉ?
λλ μ°λ¦¬κ° μ΄κ²μ λν λͺ μμ μΈ ν μ€νΈλ₯Ό κ°μ§κ³ μλ€κ³ μκ°νμ΅λλ€.
λλ μ΄κ²μ΄ λ²κ·ΈλΌκ³ μκ°ν©λλ€. μ°λ¦¬λ μ΄κ²μ λν ν
μ€νΈλ₯Ό κ°μ§κ³ μμ§λ§ μ΄κΈ° μ± λΆν
μ΄ μλ transitionTo
μ 컨ν
μ€νΈμμλ§ μμ΅λλ€.
μμ : http://jsbin.com/mejeto/1#/about
μ΄ λ²κ·Έ https://github.com/emberjs/ember.js/pull/5473μ λν μ€ν¨ ν
μ€νΈλ₯Ό μΆκ°νμ΅λλ€.
μ΄ λ¬Έμ λ₯Ό κ³ μΉλ λ° μ°λ₯΄λ €κ³ ν κ²μ
λλ€.
κ΄λ ¨ λ¬Έμ λ₯Ό μ°μ°ν λ°κ²¬ ν κ² κ°μ GitHubμμ λ³λμ ν°μΌμ΄ νμνμ§ νμ€νμ§ μμ΅λλ€.
export default Ember.Controller.extend({
queryParams: ['modal'],
modal: null,
handleModal: function () {
this.send('openModal', this.get('modal'));
}.observes('modal')
});
export default Ember.Route.extend({
actions: {
openModal: function (name) {
return this.render(name, {
into: 'application',
outlet: 'modal'
});
}
}
});
μ΄κ²μ Error while processing route: index Nothing handled the action 'openModal'. If you did handle the action, this error can be caused by returning true from an action handler in a controller, causing the action to bubble.
λμ§ λ λΆν
μ μ μΈν λͺ¨λ κ²½μ°μμ μλν©λλ€.
Ember.run.next
μ¬μ©νμ¬ μμ ν μ μμ΅λλ€.
handleModal: function () {
Ember.run.next(this, function () {
this.send('openModal', this.get('modal'));
});
}.observes('modal')
νλ¦° κ² κ°μ§λ§.
JSBins :
Ember.run.next κ°
Ember.run.nextλ₯Ό μ¬μ©νλ©΄ μ½μμμ 'λ€μ νμ'λ©λλ€.
@ Igor10k λλμ΄ λκ°μ λ¬Έμ , λͺ¨λ¬ λ±μ
λꡬλ μ΄κ²μ΄ 1.8μμ μ¬μ ν λ¬Έμ μμ νμΈν μ μμ΅λκΉ?
@wagenet yes μ΄κ²μ 1.8μμ μ¬μ ν λ¬Έμ μ
λλ€.
http://emberjs.jsbin.com/lezuhu/1/edit?html , js, output
κ·Έλ¦¬κ³ μΉ΄λ리μ :
http://emberjs.jsbin.com/doyohe/1/edit?html , js, output
@machty- μκ°?
λλ μ΄κ²μ΄ 1.8.1μ μ¬μ©νλ λμκ²λ λ²κ·ΈλΌκ³ μκ°ν©λλ€. λ΄κ° μνλ κ²μ νΉμ queryParamμ΄ λ³κ²½ λ λλ§λ€ 컨νΈλ‘€λ¬μμ νΉμ λ©μλλ₯Ό νΈμΆνλ κ²μ
λλ€. μ΄λ° λͺ¨μμ λΌμ°ν°κ° μμ΅λλ€.
μ΄λ€ μΆ©κ³ ?
// ...snip...
// for pagination
queryParams: {
offset: {
refreshModel: true
}
},
model: function(params) {
// returns model
},
setupController: function(controller, model) {
this._super(controller, model);
controller.set('model', model);
// Fetch results is a method that does a bunch work
// sets up some filters, generates a promise, resolves that promise
// and then sets some array data on the controller once promise resolves.
// offset is a pagination feature which determines which specific results
// are fetched. I would like to be able to call fetchResults everytime the
// offset queryParam is changed.
Ember.run.later(function() {
controller.sendAction('fetchResults');
})
}
// ...snip...
@machty- μ΄κ²μ λν νκ³ λ₯Ό μμν μμΉμ λν μμ΄λμ΄?
λλ κ°μ λ¬Έμ κ° μμ΅λλ€. 컨νΈλ‘€λ¬μ this.set ()μ URLμ 쿼리 λ§€κ° λ³μλ₯Ό μ λ°μ΄νΈνμ§ μμ΅λλ€.
App.SearchController = Ember.Controller.extend ({
queryParams : [ 'λλ©μΈ'],
λλ©μΈ : null,
domainField : Ember.computed.oneWay ( 'λλ©μΈ'),
μμ
: {
searchSubmit : function () {
this.set ( 'domain', this.get ( 'domainField')); // 'λλ©μΈ'λ§€κ° λ³μλ₯Ό μ€μ νμ§λ§ URLμ μμ νμ§λ μμ΅λλ€. λ°λΌμ App.SearchRouteμ λͺ¨λΈμ μμλλ‘ νΈμΆλμ§ μμ΅λλ€.
}
},
});
App.SearchRoute = Ember.Route.extend ({
queryParams : {
λλ©μΈ: {
refreshModel : true // μ 체 μ ν μ ν
},
},
λͺ¨λΈ : function (params) {
var λλ©μΈ = params.queryParams.domain;
return $.getJSON('/search?domain=usv.com&name=').then(function(resp){
console.log(resp);
return resp.data;
});
},
μμ
: {
μλ‘ κ³ μΉ¨ : function () {
Ember.Logger.log ( 'Route is now refreshing ...');
this.refresh ();
}
},
});
μ μ λμ ν΄κ²° λ°©λ²μ μ κ³΅ν΄ μ£Όμκ² μ΅λκΉ?
λͺ νννκΈ° μν΄ : λ΄ μ½λμ this.set ()μ Class κ°μ²΄λ₯Ό λ³κ²½νμ§λ§ URLμ μ ν λ³κ²½νμ§ μμ΅λλ€.
μ΄κ²μ κ³ λμ΄μ§λ§ μ¬μ ν λ¬Έμ μΈ κ² κ°μ΅λκΉ? μν©μ νμ
ν μλ μμ§λ§ 컨νΈλ‘€λ¬μμ 쿼리 λ§€κ° λ³μλ₯Ό μ€μ νλ €κ³ νλ©΄ μ£ΌκΈ°μ μΌλ‘ URLλ‘ μ νλμ§ μμ΅λλ€. 컨νΈλ‘€λ¬μμ μμ± κ°μ νμΈν μ μκ³ μ¬λ°λ₯΄κ² μ€μ λμ΄ μμ§λ§ URLμ΄ λ³κ²½λμ§ μμμ΅λλ€. Ember.run.next
μ (λ₯Ό) μ¬μ©ν΄ 보μμ΅λλ€. λΉμ !
μ΄ λ¬Έμ λ κ²½ν
@rwjblue : μ΄κ²μ λν μ λ°μ΄νΈκ° μμ΅λκΉ?
λͺ¨λ μ λ°μ΄νΈ? μ¬κΈ°μμ 컨νΈλ‘€λ¬μμ κ°μ λ³κ²½ν λλ urlμ λͺ ms λμ μ λ°μ΄νΈλκ³ μ»¨νΈλ‘€λ¬κ° λ³μμ μ κ°μ΄ μλλΌλ μ΄μ κ°μΌλ‘ λμκ°λλ€.
ν΄κ²° λ°©λ²μ λ€μκ³Ό κ°μ΅λλ€.
컨νΈλ‘€λ¬μμ μ€μ λ ν λ€μ μ€ν 루νμμ 쿼리 λ§€κ° λ³μλ₯Ό λ³κ²½νλ κ΄μ°°μλ₯Ό μΆκ°ν©λλ€. μ΄ μμμλ URLμμ token
쿼리 λ§€κ° λ³μλ₯Ό μ κ±°νκ³ μΆμκ³ μ΄ μ κ·Ό λ°©μμ μ μλν©λλ€.
import Ember from 'ember';
export default Ember.Controller.extend({
queryParams: ['token'],
token: null,
unsetToken: Ember.observer('token', function() {
if (this.get('token')) {
Ember.run.next(this, function() {
this.set('token', null);
});
}
}),
});
μ€λ λΆν μ setupControllerμμ 쿼리 λ§€κ° λ³μλ₯Ό μ€μ νλ €κ³ ν λ μ΄κ²μ λ§λ¬μ΅λλ€. run.nextλ ν΄κ²° λ°©λ²μ²λΌ 보μ΄μ§λ§ μ μ ν ν΄κ²°μ± μ 2 κ°μ κ²½λ‘λ₯Ό κ°λ κ²μ΄λ©° λΆν μ 첫 λ²μ§Έ κ²½λ‘λ 쿼리 λ§€κ° λ³μ κ°μ μ¬μ©νμ¬ λ λ²μ§Έ κ²½λ‘λ‘ μ ν ν κ²μ λλ€.
μ λ² 2.8
λΆλ₯ μ μ± μ λ°λΌ λ¬Έμ λ₯Ό λΉνμ±μΌλ‘ νμν©λλ€.
λλ Ember 2.11μμμ΄ λ¬Έμ λ₯Όλ³΄κ³ μλλ°, 컨νΈλ‘€λ¬μμ 쿼리 λ§€κ° λ³μλ₯Ό μ§μ°λ €κ³ ν΄λ ν¨κ³Όκ° μμ΅λλ€.
νΈμ§ : μ΄ λͺ¨λ μ¬μ¦λ₯Ό 무μνμμμ€. μ¬κΈ°μλ λͺ κ°μ§ μλͺ»λ / μλͺ»λ μ λ³΄κ° μμΌλ©° @locks λ λ°λ‘ μλμ λμ μΈκ·Έλ¨ΌνΈ λμ 쿼리 λ§€κ° λ³μλ₯Ό μ¬μ©νλ ν¨μ¬ λ λμ μ루μ
μ μ μνμ΅λλ€. get
μμ μμ λ° μ€μ νλ μλμ° κ³μ° μμ±μ μ¬μ©νμ¬ μΏΌλ¦¬ λ§€κ° λ³μλ₯Ό μΌλ° μμ±μ λ°μΈλ©ν©λλ€ (μ΄μ μλμλ 쿼리 λ§€κ° λ³μλ₯Ό set
μ μμ λκ³ μ€μ λ κ³μ° λ μμ±μ λ°μΈλ©νλ κ²μ΄ ν¬ν¨λμμ΅λλ€) . μμ μ£μ‘ν©λλ€!
λ λμ μ루μ
...
// app/router.js
Router.map(function() {
this.route('shop');
});
// app/routes/shop.js
export default Ember.Route.extend({
queryParams: {
_selectedItemIndex: {
replace: true
}
}
});
// app/controllers/shop.js
import computed from 'ember-macro-helpers/computed';
export default Ember.Controller.extend({
cart: Ember.service.inject(),
queryParams: {
_selectedItemIndex: 'i'
},
_selectedItemIndex: 0,
selectedItemIndex: computed('_selectedItemIndex', {
get(index) {
const itemsLength = this.get('cart.items.length');
if (isNaN(index) || index < 0 || index >= itemsLength) {
index = 0;
}
return this.set('_selectedItemIndex', index);
},
set(index) {
return this.set('_selectedItemIndex', index);
}
})
});
μλ λ¬Έμ μ ν΄κ²°μ±
...
λλ 2.14- λ² νμμ μ¬μ ν μ§μλλμ΄ λ¬Έμ λ‘ μ΄μ μ λ λμ λΆλΆμ μμμ΅λλ€. λ΄ μ¬μ© μ¬λ‘λ Ember.Arrayμ μΈλ±μ€λ‘ μ¬μ©ν 쿼리 λ§€κ° λ³μλ₯Ό μμ νλ κ²μ λλ€. νμμ΄ μλͺ»λμκ±°λ (μ : NaN λλ λ¬Έμμ΄) λ²μλ₯Ό λ²μ΄λ κ²½μ° κΈ°λ³Έ μμΈμ μ¬μ©νκ³ μ‘°μ μ λ°μνλλ‘ URLμ μ λ°μ΄νΈν΄μΌν©λλ€ (ν₯ν ν΄λ¦μ λν μλ΅ μ΄λ²€νΈκ° μ μ νκ² νΈλ¦¬κ±°λλλ‘ μΆκ°λ‘ μ€λͺ ν μ μμ). μ΄κ²μ΄ λ¨μν μΈκ΄μμ λ¬Έμ κ° μλλΌλ μ¬μ€μ μ μ¦νλ λ° λμμ΄λ©λλ€).
μ΄ λ¬Έμ μ μ€λͺ
λλλ‘ URLμ μ
λ°μ΄νΈνμ§ μμΌλ―λ‘ setupController()
μμ μμ±μ μ€μ ν μ μμ΅λλ€. Ember.runμ μ¬μ©νμ¬ λ€μ ν±μμ μ€μ ν μ μμ΅λλ€. λ무 λ¦κ² μ μ©λκΈ° λλ¬Έμ
λλ€ (μ¦, μλͺ»λ μΈλ±μ€κ° μ΄λ―Έ λ°°μ΄μ μ‘μΈμ€νλ λ° μ¬μ©λμμ κ²μ
λλ€). μμ± λ³κ²½μ΄ μκΈ° λλ¬Έμ ν λ² μ€μ ν λ€μ URLμ μ
λ°μ΄νΈνκΈ° μν΄ λ€μ ν±μμ λ€μ μ€μ ν μ μμ΅λλ€ (λλ μ μ΄λ μ΄κ²μ΄ μλνμ§ μλ μ΄μ λΌκ³ μκ°ν©λλ€).
쿼리 λ§€κ° λ³μλ₯Ό κ³μ° λ μμ±μ λ°μΈλ© ν μ μκΈ° λλ¬Έμ 컨νΈλ‘€λ¬μμ μμ μ€μ μμ ν¨κ» κ³μ° λ μμ±μ μ¬μ©ν μ μμ΅λλ€. κ΄μ°°μ€μΈ μμ±μ μ€μ ν μ μκΈ° λλ¬Έμ μΌλ° μμ±μ κ΄μ°°μλ₯Ό μ¬μ©ν μ μμ΅λλ€. μμμ μ€λͺ ν λ¬Έμ λ‘ μΈν΄ Ember.runμ μ΅μ λ²λ₯Ό μ¬μ©ν μ μμ΅λλ€.
# 14606 λ° κ΄λ ¨ λ¬Έμ λ‘ μΈν΄ setupController()
this.transitionTo({ queryParams: .. })
μμ setupController()
λλ beforeModel()
νν¬ λλ controller.transitionToRoute({ queryParams: .. })
μμ setupController()
ν μ μμ΅λλ€. μ΄ μΏΌλ¦¬ λ§€κ° λ³μκ° λ³κ²½λλ©΄ λ°λ³΅λμ§ μμμΌνλ model()
νν¬μμ λ€λ₯Έ μμ
μ΄ λ°μνκΈ° λλ¬Έμ 쿼리 λ§€κ° λ³μμ λν΄ refreshModel
μ true
λ‘ μ€μ ν μ μμ΅λλ€.
λΆλͺ ν μ¬κΈ°μλ κ³ ν΅μ κ²°ν©μ΄ μμ΅λλ€. μ΄ λ¬Έμ , λ¬Έμ # 14606 λ° μΏΌλ¦¬ λ§€κ° λ³μλ₯Ό κ³μ° λ μμ±μ λ°μΈλ© ν μ μλ€λ μ νμ΄ λͺ¨λ κ²°ν©λμ΄ κΉλνκ³ μ€λ§μ€λ¬μ΄ μμ ν¨μ μ λ§λλλ€.
ν΄κ²° λ°©λ²μ λ€μκ³Ό κ°μ΅λλ€. λ¨μν 쿼리 λ§€κ° λ³μ (λλ μλ μ½λμμμ κ°μ΄ λμ μΈκ·Έλ¨ΌνΈ)λ₯Ό κ°μ Έ μμμ΄λ₯Ό μμ νκ³ λΆλͺ¨ 컨νΈλ‘€λ¬μμ μμ±μ μ€μ νλ μ€μ²© λ "μ ν"κ²½λ‘λ₯Ό λ§λ€μμ΅λλ€. μμ λ μΈλ±μ€κ° κ²½κ³λ₯Ό λ²μ΄λ κ²½μ° μ€μ²© λ μΈλ±μ€ κ²½λ‘λ‘ μ νλκ³ , μ°¨λ‘λ‘ κΈ°λ³Έ μΈλ±μ€κ°μλ μ€μ²© λ μ ν κ²½λ‘λ‘ λ€μ μ νλ©λλ€. μ€μ²© λ κ²½λ‘μλ ν νλ¦Ώμ΄λ 컨νΈλ‘€λ¬κ°μκ³ μμ κ²½λ‘μλ μ½μΌνΈκ° μμ΅λλ€. μ΄ λΌμ°ν λ¬Έμ λ₯Ό μ²λ¦¬νκΈ° μν΄μλ§ μ‘΄μ¬ν©λλ€.
// app/router.js
Router.map(function() {
this.route('shop', function() {
this.route('index', { path: '/' });
this.route('select', { path: '/:index' });
});
});
// app/routes/shop.js
export default Ember.Route.extend({
setupController(controller, model) {
this._super(...arguments);
// some logic to set up the cart service
},
model() {
// fetch products to shop
}
});
// app/controllers/shop.js
export default Ember.Controller.extend({
cart: Ember.service.inject(),
selectedItemIndex: 0,
actions: {
selectItem(index) {
return this.replaceRoute('shop.select', index);
}
}
});
// app/routes/shop/index.js
export default Ember.Route.extend({
beforeModel() {
return this.replaceWith('shop.select', 0);
}
});
// app/routes/shop/select.js
export default Ember.Route.extend({
setupController(_, { index }) {
this._super(...arguments);
const
parentController = this.controllerFor('shop'),
itemsLength = parentController.get('cart.items.length');
index = parseInt(index, 10);
if (Number.isNaN(index) || index < 0 || index >= itemsLength) {
return this.replaceWith('shop.index');
}
parentController.set('selectedItemIndex', index);
},
model: _ => _
});
@mwpastore μ λ°μ΄νΈμ κ°μ¬λ립λλ€. μ’μ μ£Όλ§ λ³΄λ΄μΈμ!
@locks μνλ κ²½μ° λ¬Έμ λ¬Έμ λ₯Ό λ€μ μ΄μ΄ setupController
μμ΄ λ₯Ό κΈ°λ‘νμμμ€ (μλ‘
@mwpastore https://discuss.emberjs.com/ μ ν¬λΌμ κ·νμ μ루μ μ λν κ²μλ¬Όμ μΆκ° ν μμλ κ² κ°μ΅λλ€.
μ΄ λ¬Έμ λλ³΄κ³ νλ λ²κ·Έλ₯Ό ν΄κ²°νκΈ°μν λ체 μ루μ μΌλ‘ μ’ κ²°λμμΌλ©° ν΄λΉ μ루μ μλ κ³μ° λ getterμμ μμ± μ€μ μ΄ ν¬ν¨λ©λλ€.
μ΄κ²μ λν μ λ°μ΄νΈκ° μλμ§ κΆκΈν©λλ€.
Routeμ setupController
λ΄μμ Ember.run.next
μμ΅λλ€.
κ°μ₯ μ μ©ν λκΈ
λλ Ember 2.11μμμ΄ λ¬Έμ λ₯Όλ³΄κ³ μλλ°, 컨νΈλ‘€λ¬μμ 쿼리 λ§€κ° λ³μλ₯Ό μ§μ°λ €κ³ ν΄λ ν¨κ³Όκ° μμ΅λλ€.