vendredi 7 août 2015

Adding object to array using controllers model

Hello i'm trying to add an object to an array using ember. The friends controller model returns an array of objects (App.friends).

From the friends/new template I get a users firstName,lastName and about them.

Once I've gotten that the user clicks create and the information is added to App.friends through using the needs feature of ember to access App.friends from inside the FriendsNewController.

For some reason im getting the following error when i click create,

Uncaught TypeError: desc.get is not a function

App.FriendsRoute = Ember.Route.extend({
    model: function(){
        return App.friends;
    }
});


App.FriendsNewController = Ember.Controller.extend({
    needs: "friends",
    isInvalid: true,

    validForm: function(){

        if(this.get('lastName') && this.get('firstName')){
            this.set("isInvalid", false);
        } else {
            this.set("isInvalid", true);
        }

    }.observes('firstName','lastName'),

    actions: {
        create: function(){
            var newFriend = Ember.copy(this.content);
            this.get('controllers.friends').addObject(newFriend);
            this.transitionToRoute('friends');
        }
    }
});

  <script type="text/x-handlebars" id="friends/new">
    <label>First Name</label>
    {{input value=firstName}}<br />

    <label>Last Name</label>
    {{input value=lastName}}<br />

    <label>About</label>
    {{textarea value=about}}<br />

    <button {{action "create"}} {{bind-attr disabled=isInvalid}}>Create</button>

  </script>

Im extremely new to Ember, so feel free to push me in the right direction. Thanks :)




Aucun commentaire:

Enregistrer un commentaire