user2942566
user2942566

Reputation: 455

How to edit a record from a list view using backbone Js

This is an EDIT to what i had posted earlier. I tired out few things suggested in this post. i think im close to getting it, but needs some more tweak.

I am looking to edit a record from my list:This is how my view is designed:

var EditSubAccount = Backbone.View.extend({
     render: function(id,options){  
            var self= this; 
            
                var subaccount = new SubAccountModel([],{id: this.options});
                subaccount.fetch({
                    
                    success: function(subaccount){
                        var d = subaccount.toJSON();
                        var user = d.data.items;
                       //user gives the Json o/p as shown below
                        var template = $("#sub-account-list").html(_.template(edittmpl, {user:user}));
                        
                    
                })
            }

Here the user gives the Json object as follows:

[
 {
    accountType: "Customer"
    active: true
    name: account 1
    id: 87384738
    billingContactInfo: 
    {
       address1: "1234 asdf"
       address2: ""
       city: "mountain view"
     }
   }
 ]

and my html:

<form class="edit-form" method="PUT" >
        <table>
            <tbody>

        <tr>
       <td> <input type = "text"class="name" value= "<%= user ? user.name : '' %>" /></td>
     <!-- get undefined when on `user.name` -->
    </tr>

    </tbody>
    </table>
    <% if (subaccount){ %>

    <input type="hidden" value = "<%= user.id %>" />

    <% } %>
     
        <button class="save" method="PUT">Save</button>
        <button class="cancel">Cancel</button>
    </form>

and my router where im initializing the Editaccount:

var AppRouter = Backbone.Router.extend({
  routes: {
      'edit/:id' : 'editSubAccount',
}
  editSubAccount: function(id){
   var editsubaccount = new EditSubAccount(id); 
        editsubaccount.render({id:id});
  }
  
  });

When i try click the edit link it does display the correct 'id' i need to edit, but the form displays blank instead populating with the fields. below is how my list looks like: enter image description here

I am not sure what im missing in this. Please anyone any ideas?

Upvotes: 1

Views: 330

Answers (1)

rcarvalho
rcarvalho

Reputation: 790

Try to do 'subacount.toJSON()' when you bind the model with the template, it could be the problem.

But take care with your render function, it is doing a lot of things, as your code is growing up, it can be impossible to maintain.

I have a post about it, take a look here to help with this issue http://www.rcarvalhojs.com/dicas/de/backbone/2014/06/04/5dicas-backbone.html

unfortunately it's only available in portuguese yet. try to use google to translate

Hope it helps.

Upvotes: 1

Related Questions