将数据从 Ember 组件向上传递到模型

Passing data up from an Ember compontent to model

我是 Ember 的新手,我正在尝试弄清楚如何最好地使用组件来更改模型。我试图弄清楚我在 Ember 1.13.7 上并使用 FixtureAdapter。我有以下代码:

app/models/client.js

import DS from 'ember-data';

export default DS.Model.extend({
    name: DS.attr('string'),
    clientCode: DS.attr('string'),
    vatNr: DS.attr('string'),
    regNr: DS.attr('string'),
    address: DS.attr('string'),
    city: DS.attr('string'),
    postCode: DS.attr('string'),
    country: DS.attr('string'),
    lastViewed: DS.attr('date')
}).reopenClass({
  FIXTURES: [
    { id: 1, name: 'Con', clientCo: 'CON01', lastViewed: new Date("2015-03-25T12:00:00") },
    { id: 2, name: 'Moi', clientCo: 'MOI01', lastViewed: new Date("2015-01-25T12:00:00") },
  ]
});

app/routes/clients.js

import Ember from 'ember';

export default Ember.Route.extend({
    model: function(){
        return this.store.find('client');
    },
    actions: {
        saveClient: function() {
            var model = this.modelFor('clients/show');
            model.save().then(()=> {
                this.transitionTo('clients');
            });
        }
    }
});

app/templates/clients/edit.hbs

<div class="row">
    <div class="col-md-6">
        <form class="form-horizontal">
            {{bootstrap-input model.name "Name"}}
            {{bootstrap-input model.email "Email"}}
            <div class="text-right">
                <button class="btn btn-primary" {{action 'saveClient'}}>Save</button>   
            </div>
        </form>
    </div>
</div>

app/compontents/bootstrap-input.js

import Ember from 'ember';

export default Ember.Component.extend({
    positionalParams: ['model', 'label']
});

app/templates/compontents/bootstrap-input.hbs

<div class="form-group">
    <label class="col-sm-2 control-label">{{label}}</label>
    <div class="col-sm-10">
        {{input value=model type="text" class="form-control" placeholder=placeholder}}
    </div>
</div>

现在如您所见,更改不会反映在向下传递的模型中,因为它是一种单向绑定(我认为这是怎么回事?),或者更确切地说不直接引用模型.当我不使用 positionalParams 并将模型直接传入组件时,它工作正常。例如:

{{bootstrap-input model=model.name label="Name"}}

但据我所知,这不是在 Ember 中执行此操作的正确方法 2. 如何将组件中所做的更改应用到我的模型中,以便将其保存在路径中?

您需要在组件中将属性引用为 attrs.model 而不是 model

See working demo.