0
這個想法是填充一個表單,填充當前路由的對象屬性,使用戶可以更新屬性並提交更改。但是,我似乎無法訪問控制器中的屬性,即使我可以讓它們在模板中顯示出來。由於我使用Ember-CLI,因此以下是相關文件。Ember編輯表單模板填充和更新值
我找到了控制器的Ember文檔,討論如何使用setupController設置路徑中控制器的模型。但是,這並沒有奏效。現在我非常想法。
如果您想以更清晰的形式看到它,GitHub Repo也是最新的。
路由器
// router.js
import Ember from 'ember';
var Router = Ember.Router.extend({
location: EmberWbsENV.locationType
});
Router.map(function() {
this.route('index', { path: '/' });
this.route('items');
this.resource('edit', { path: 'items/:item_id/edit' });
this.route('new', { path: 'items/new' });
});
export default Router;
路線
// routes/edit.js
import Ember from 'ember';
export default Ember.Route.extend({
model: function(params){
return this.store.find('item', params.item_id);
},
setupController: function(controller, item) {
controller.set('model', item);
}
});
控制器
// controllers/edit.js
import Ember from 'ember';
export default Ember.ObjectController.extend({
// variables for form values
wbsCode: this.get('model.code'),
wbsAbbrev: this.get('model.abbrev'),
wbsDesc: this.get('model.desc'),
wbsIsSuffix: this.get('model.isSuffix'),
actions: {
// exit without changing anything
cancel: function() {
this.transitionToRoute('items');
},
// process new wbs item submissions
save: function() {
// set values from form to current instance model
this.set('code', this.wbsCode);
this.set('abbrev', this.wbsAbbrev);
this.set('desc', this.wbsDesc);
this.set('isSuffix', this.wbsIsSuffix);
// save item instance into data store and return to items list
this.save().then(function() {
this.transitionToRoute('items');
});
},
// remove the current wbs item
remove: function() {
// TODO: create delete/remove method
this.transitionToRoute('items');
}
}
});
模板
{{! templates/edit.js }}
<form class="form-horizontal" role="form">
<div class="col-sm-offset-2">
<h1>Edit {{abbrev}}</h1>
</div>
<div class="form-group">
<label for="wbs-code" class="control-label col-sm-2">Code</label>
<div class="col-sm-2">
{{input type="text" class="form-control" id="wbs-code" placeholder="C04416" tabindex="1" autofocus="true" value=wbsCode}}
</div>
</div>
<div class="form-group">
<label for="wbs-short" class="control-label col-sm-2">Short Description</label>
<div class="col-sm-2">
{{input type="text" class="form-control" id="wbs-short" placeholder="ARC4" tabindex="2" value=wbsAbbrev}}
</div>
</div>
<div class="form-group">
<label for="wbs-long" class="control-label col-sm-2">Long Description</label>
<div class="col-sm-8">
{{input type="text" class="form-control" id="wbs-long" tabindex="3" placeholder="ArcGIS 4: Sharing Content on the Web" value=wbsDesc}}
</div>
</div>
<div class="form-group">
<label for="is-suffix" class="control-label col-sm-2">WBS code suffix</label>
<div class="col-sm-8">
{{input type='checkbox' class='glyphicon glyphicon-unchecked' id='is-suffix' tabindex='4' checked=wbsIsSuffix}}
</div>
</div>
<div class="col-md-offset-2">
<buton type="button" {{action 'cancel'}} class="btn btn-default">
<span class="glyphicon glyphicon-remove-circle"></span> Cancel
</buton>
<button type="button" {{action 'save'}} class="btn btn-primary">
<span class="glyphicon glyphicon-save"></span> Save
</button>
<button type="button" {{action 'remove'}} class="btn btn-danger">
<span class="glyphicon glyphicon-warning-sign"></span> Delete
</button>
</div>
</form>