0
votes

I am working on a simple CRUD app using ember js. I have a template with a input form which sends data to the server and server responds with success response.

input form data: "user": {"id": 1, "name": "John"};
server response: {"message":"Created successfully", "nextId": 2}.

I want to display the success message on the same template(one with the input form). Because template refers to the user model and user model does not have a message attribute. How can it be done? Please help.

Here is my code:

var Manager = Ember.Application.create();

Manager.Router.map(function() {
   this.resource('users');
   this.resource('user', {path: '/users/:user_id'});
   this.resource('home', {path: '/'});
});


Manager.UserRoute = Ember.Route.extend({
   model: function(params) {
       return jQuery.getJSON("http://localhost:8085/users/"+ params.user_id);
   },

   actions :{
       insert : function(){
           var user = this.modelFor("User");
           $.ajax({
                url: "http://localhost:8085/Users", 
                data: JSON.stringify(user),
                type: "POST",
                contentType: "application/json",
           })
           .done(function(data){
            //set data.message to display on UI 
           })
       }
   }
});

and my template looks like this:

<script type="text/x-handlebars" data-template-name="user">
<form class="form-horizontal" role="form" {{action "insert" on="submit"}}>
    <div class="form-group">
        <label for="userId" class="control-label col-xs-2">User Id</label>
            <div class="col-xs-4 input-group">
                {{input type="number" value=_id class="form-control" id="userId" placeholder="Identity"}}
            </div>
    </div>

    <div class="form-group">
        <label for="name" class="control-label col-xs-2">Name</label>
            <div class="col-xs-4 input-group">
                {{input type="text" value=name class="form-control" id="name" placeholder="Name"}}
            </div>
    </div>

    {{#if message}}
        <div class="alert alert-success input-group" role="alert">{{message}}</div>
    {{/if}} 

    <div class="form-group button-center">
        <button type="submit" class="btn btn-default">Submit</button>
    </div>
</form>
</script>
1
Can you provide more of your code? Templates, Controllers, etc? - Oren Hizkiya
@Oren, I have added my template and js. - ans7991

1 Answers

0
votes

Set the message property on your controller to the response that comes back.

actions :{
       insert : function(){
           var user = this.modelFor("User");
           var self = this;
           $.ajax({
                url: "http://localhost:8085/Users", 
                data: JSON.stringify(user),
                type: "POST",
                contentType: "application/json",
           })
           .done(function(data){
               //set data.message to display on UI 
               self.controllerFor('user').set('message', data.message);
           })
       }
   }