0
votes

The idea is to have a form populated with the current route's object properties enabling a user to update the properties and submit the changes. However, I cannot seem to access the properties in the controller, even though I can get them to show up in the template just fine. Since I am using Ember-CLI, here are the relevant files.

I found the Ember documentation for controller discussing setting the model for the controller in the route using setupController. This however, did not work. Now I am pretty out of ideas.

If you want to see it in a slightly more legible form, the GitHub Repo is also current.

Router

// 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;

Route

// 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);
    }
});

Controller

// 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');
        }
    }
});

Template

{{! 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>
1

1 Answers

0
votes

Not a full answer but may get you started:

From the ember docs:

If [a form's attributes ]attributes are set to a quoted string, their values will be set directly on the element However, when left unquoted, these values will be bound to a property on the template's current rendering context. For example:

Here's an example of a form wich may do something similar:

{{input type="text" placeholder=OriginalValue value=newValue action="submitProperty"}}

<button class="btn btn-default" name='commit' {{action 'submitProperty' newValue}}

I think you may want the form to have a placeholder with the inital property value for a given property. And then an action which pipes the new values entered into the form into a controller action which updates the properties.

Also consider trying to set the problem up in a js.bin: http://jsbin.com/ and setting up the problem in there to get better responses.