Permalink
Browse files

bind path through many properties

  • Loading branch information...
collin committed Apr 10, 2012
1 parent a399ce7 commit 98663df8fba403e38b5e5ca738d937c8794832e3
Showing with 28 additions and 2 deletions.
  1. +9 −1 lib/alpha_simprini/core/properties/has_many.coffee
  2. +19 −1 test/core/properties/has_many.coffee
@@ -1,14 +1,22 @@
AS = require("alpha_simprini")
_ = require("underscore")
-AS.Model.HasMany = AS.Model.Field.extend()
+AS.Model.HasMany = AS.Model.Field.extend ({delegate, include, def, defs}) ->
+ def couldBe: (test) ->
+ return true if test in (@options.model?()?.ancestors ? [])
+ @_super.apply(this, arguments)
+
AS.Model.HasMany.Instance = AS.Model.Field.Instance.extend ({def, delegate}) ->
delegate AS.COLLECTION_DELEGATES, to: "backingCollection"
def initialize: (@object, @options={}) ->
@options.source = @object if @options.inverse
@backingCollection = AS.Collection.new(undefined, @options)
+ def bindToPathSegment: (segment) ->
+ segment.binds this, "add", segment.insertCallback
+ segment.binds this, "remove", segment.removeCallback
+
def set: (models) ->
@backingCollection.add models
@@ -6,10 +6,12 @@ NS.Parent = AS.Model.extend()
NS.Parent.hasMany "children"
NS.Inverter = AS.Model.extend()
-NS.Inverter.hasMany "children", inverse: "parent"
+NS.Inverter.hasMany "children", inverse: "parent", model: NS.Child
NS.Child = NS.Parent.extend()
NS.Child.hasMany "children", model: -> NS.Child
+NS.Child.field "power"
+NS.Child.field "toughness"
NS.Child.property "parent"
exports.HasMany =
@@ -43,6 +45,22 @@ exports.HasMany =
test.done()
+ "bindPath":
+ "may bind through HasMany by name": (test) ->
+ test.expect(2)
+ o = NS.Inverter.new()
+ o.bindPath ['children', 'children', 'power'], -> test.ok true
+
+ child = o.children.add NS.Child.new()
+
+ child.children.add().power.set("howdy")
+ child2 = child.children.add()
+ child2.power.set("howdy as well")
+
+ child.children.remove(child2)
+ child2.power.set("orhpan power")
+ test.done()
+
"Sharing":
"propagate values from model to share on sync": (test) ->
o = NS.Parent.new()

0 comments on commit 98663df

Please sign in to comment.