Rocky
Rocky

Reputation: 401

How do you do two-way binding use variables?

When a create a view from an object, it seems that when I change a property in the object, the view's property also changes. If I change the property in the view, the change is not reflected in the object. I thought two-way bindings were the default behavior. Am I missing something?

WidgetClass = Ember.Object.extend
  address: 'widget address'
  create_view: ->
    # console.log this.name
    view = Ember.View.create
      someobj: this
      addressBinding: 'someobj.address'
      template: Ember.Handlebars.compile '{{address}}'

    return view

TextWidget = WidgetClass.create()

view = TextWidget.create_view()

view.append()

view.set 'address', 'new new address'
console.log (view.get 'address')
console.log (TextWidget.get 'address') # I am expecting this output to be 'new new address'

http://jsfiddle.net/rkitamura/2zsUX/

Upvotes: 1

Views: 520

Answers (2)

lilyfrommars
lilyfrommars

Reputation: 41

It is important to use setPath in this case. Two-way binding works perfectly.

The following code works.

view.setPath 'someobj.address', 'new new address'
console.log (TextWidget.get 'address')  # this outputs 'new new address' correctly

Upvotes: 1

Dan Gebhardt
Dan Gebhardt

Reputation: 3281

When applying bindings and then testing them immediately, you need to force the Ember run loop to catch up. Try adding Ember.run.sync() before view.set 'address', 'new new address'. Your setTimeout() call should no longer be needed.

Upvotes: 2

Related Questions