Passing data up from an Ember compontent to model

334 Views Asked by At

I'm new to Ember, and I'm trying to figure out how to best use components to change a model. I'm trying to figure out I'm on Ember 1.13.7 and using the FixtureAdapter. I have the following code:

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>

Now as you can see, changes won't be reflected in the model being passed down, since it's a one way binding (i think this is what's going on?), or rather doesn't reference the model directly. When I don't use positionalParams, and pass in the model directly into the component, it works fine. Eg:

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

But as far as I understand, that's not the correct way to do it in Ember 2. How do I get the changes made in my component to my model, so I can save it in the route?

1

There are 1 best solutions below

5
On BEST ANSWER

You need to reference attribute as attrs.model instead of model in component.

See working demo.