2016-09-21 3 views
0

컨트롤러를 가지고 hbs에서 값을 가져와 선택된 국가 값을 보냅니다. 모델에서이 선택된 국가가 필요하고 계산 결과를 다시 hbs로 돌려 보내야합니다. 이 값을 컨트롤러에서 어떻게 설정하고 모델에서 가져 와서 그 값을 사용하여 계산할 수 있습니까?emberjs에서 컨트롤러에서 모델로 데이터 전달

+0

샘플 코드는 문제를보다 명확하게 설명합니다. 컨트롤러에서 모델 속성을 업데이트하는 계산 된 속성을 찾고있는 것 같아요. – kumkanillam

답변

0

글쎄, 이것을 달성하기위한 몇 가지 다른 접근법이있을 수 있습니다. 그러나, 나는 당신에게 희망을 갖고 도움이 될 몇 가지 예를 드릴 것입니다. 대신 컨트롤러의 예는 것

//Controller.js 
notes: Ember.computed('model.notes.[]', '[email protected]', function() { 
    return this.get('model.notes').sortBy('date').reverse(); //This is an example of Computed function which in this case it's sorting notes based on date. 
}), 
blink: null, 
    actions: { 
    taskChangeColor: function() { 
     this.set('blink', 'blinker'); // this is another example that set new data by action which can be retrive from model and set to property 
    } 
    } 

또는 당신이 할 수있는 또 다른 한가지는 또한

// model.js which is using ember-data and moment 
    timeZone: DS.attr(), //for example one property coming from server 
    utcOffsetFormat: Ember.computed(function() { 
    let time = moment.tz(this.get('timeZone')).format('hh:mm a'); 
    return time; 
    // using a computed function to instantiate another value based on existing model property which means you can simpley use this property instead of direct one. 
    }) 

같은 모델 자체에서 계산 기능을 사용하는 것입니다, 당신은 여전히 ​​Route.js에서 작업을 사용 자격이 있습니다 :

위의 예에서
//route.js 
actions: { 
    changeSave: function(step) { 
     var something = { 
     contact: this.currentModel, 
     }; 
     this.currentModel.set('step', something.contact); 
     this.currentModel.save().then(d => { 
     // set your alert or whatever for success promise 
     return d; 
     }).catch(e => { 
     console.log(error(e.message)); 
     return e; 
     }); 
    }, 

당신은 내가 동일한 속성 이름을 가진 모델에 쉽게 설정할 수 있습니다 모델 메모()를 저장하는 작업을 설정 한 것을 볼 수 있습니다 당신이 당신 위스콘신 할 경우 결과를 즉시 볼 수 있습니다.

당신을 도울 수 있기를 바랍니다. 내가 읽을 것을 권 해드립니다 Ember-Docs

0

귀하의 요구 사항에 대해, 당신은 선택한 국가의 값을 컨트롤러 속성이 필요 없다고 말할 것입니다. 모델 자체에이 값을 유지할 수 있습니다. 경로에서

setupController(model,transition){ 
    this._super(...arguments); //this will set model property in controller. 
    Ember.set(model,'selectedCountryValue','US'); //you can set default value 
} 

와 컨트롤러 내부

, 당신은 model.selectedCountryValue에 의존와 계산 된 속성을 만들 수 있습니다. 당신이 직접 {{model.selectedCountryValue}}을 사용할 수 있습니다, 어떤 결과 템플릿에서
result:Ember.Computed('model.selectedCountryValue',function(){ 
//compute something return the result 
} 

을 계산한다.

관련 문제