2013-06-22 16 views
0

我有一个OutputsFormView应该有一个保存和取消处理按钮上的点击事件。当点击保存按钮时,它应该收集来自子视图的所有值并将其发送给控制器,然后持续它。Emberjs:从子视图解析表单数据

outputs.js

App.OutputsCreateRoute = Ember.Route.extend({ 
    model: function() { 
     return App.Output.createRecord(); 
    }, 
    renderTemplate: function() { 
     return this.render('outputs/form', { 
      controller: 'outputsCreate' 
     }); 
    } 
}); 

App.OutputsCreateController = Ember.Controller.extend({ 
    save: function(model) { 
     // outputs empty values for title, receiver, value 
     console.log(model); 
    } 
}); 

App.OutputsFormView = Ember.View.extend({ 
    tagName: 'form', 
    classNames: ['form', 'form-horizontal'], 

    save: function(e) { 
     this.get('controller').send('save', { 
      title: this.get('title'), 
      receiver: this.get('receiver'), 
      value: this.get('value') 
     }); 
    }, 
    cancel: function(e) { 

     console.log('canceling'); 
    } 
}); 

模板

<script type="text/x-handlebars" data-template-name="outputs/form"> 
    {{#view App.OutputsFormView}} 
    <legend class="text-right">create a new output</legend> 
    <fieldset> 
     <div class="control-group"> 
     <label class="control-label" for="title">Title</label> 
     <div class="controls"> 
      {{view Ember.TextField valueBinding="view.title" placeholder="Lovely Afternoon Pizza"}} 
     </div> 
     </div> 
     <div class="control-group"> 
     <label class="control-label" for="receiver">Receiver</label> 
     <div class="controls"> 
      {{view Ember.TextField valueBinding="view.receiver" placeholder="The Goverment"}} 
     </div> 
     </div> 
     <div class="control-group"> 
     <label class="control-label" for="value">Receiver</label> 
     <div class="controls"> 
      {{view App.ValueView valueBinding="view.value"}} 
     </div> 
     </div> 
     <div class="control-group pull-right"> 
     <div class="controls"> 
      <button type="button" {{action "save"}} class="btn">save</button> 
      <button type="button" {{action "cancel"}} class="btn btn-red">cancel</button> 
     </div> 
     </div> 
    </fieldset> 
    {{/view}} 
</script> 

出于某种原因,我不能得到的子窗体的观点的价值和不幸的是我不知道我已经忘记了。 ..

Bodo

回答

0

的解决方案是使每一是的ValueBinding在控制器中可用:

App.OutputsCreateController = Ember.Controller.extend({ 
    save: function(model) { 
     var output = this.get('model'); 

     output.set('title', this.get('title')); 
     output.set('receiver', this.get('receiver')); 
     output.set('value', this.get('value')); 

     output.save(); 
    } 
}); 

这比解析出的值直接视图的更好。但是,如果你想这样做,你可以给你的视图的名称:

{{view viewName="blabla"}} 

,然后通过与父视图访问:

this.get('blabla'); 

但是我觉得值绑定方法应该是首选