Question

This page is to edit the account information.

Template file,

<input type="text" id="account_name" class="form-control" placeholder="Enter Name" value="<%=account.name%>"/>
<input type="text" id="account_company_name" class="form-control" placeholder="Enter Company Name" value="<%=account.company_name%>"/>
<a id="account_next_btn" class="btn" role="button">Next</a>

view file,

events:
    'click #account_next_btn': "updateAccount"

updateAccount: (e)->
    e.preventDefault()
    @account.save({"name": @$el.find("#account_name").val(),"company_name": @$el.find("#account_company_name").val()})

ok, this works fine. it sends the updated input form parameters.

the thing i'm curious is, there should be a better way, not setting the updated values manually like my code.

in rails backbone:scaffold it doesn't have this kind of code.

it just

@model.save()

that is all they do.

but in my code, if i just call

@account.save()

it sends the parameter, that not have been updated.

Was it helpful?

Solution

That’s because Backbone by default doesn’t include any kind of data binding library. Data binding lets you keep a model attribute synced with a form field value.

Backbone-Rails includes the simple backbone_datalink.js, which sets up a simple form binding when you create the scaffold.

There are many other binding plugins that work with Backbone, such as Backbone.ModelBinder and Rivets.js.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top