Permalink
Browse files

bind virtual properties to collection properties

  • Loading branch information...
1 parent 0cefb85 commit d3b7f847087cbc03a0b773d56c94ff0abdbe2e56 @collin committed Apr 13, 2012
@@ -84,8 +84,8 @@ AS.Collection = AS.Object.extend ({def, include, delegate}) ->
def filter: (fn) ->
AS.FilteredCollection.new(this, fn)
- def groupBy: (key) ->
- AS.Models.Grouping.new(this, key)
+ def groupBy: (key, metaData) ->
+ AS.Models.Grouping.new(this, key, metaData)
# # When an event is triggered from a model, it is bubbled up through the collection.
def _onModelEvent: (event, model, collection, options) ->
@@ -28,13 +28,13 @@ AS.FilteredCollection = AS.Collection.extend ({delegate, include, def, defs}) ->
@parent.bind
event: 'add'
- handler: @addToSelf
+ handler: @determinePlacementInSelf
context: this
namespace: @objectId()
@parent.bind
event: 'remove'
- handler: @removeFromSelf
+ handler: @determinePlacementInSelf
context: this
namespace: @objectId()
@@ -47,21 +47,23 @@ AS.FilteredCollection = AS.Collection.extend ({delegate, include, def, defs}) ->
@removeFromSelf(model)
def addToSelf: (model) ->
- model.unbind "." + @objectId()
+ return if @models.include(model).value()
+ model.unbind "."+@objectId()
model.bind
event: "change"
handler: @determinePlacementInSelf
namespace: @objectId()
context: this
- return unless @filter.get()(model) is true
@_add(model)
def removeFromSelf: (model) ->
model.unbind "." + @objectId()
- return unless @models.indexOf(model)
+ return unless @models.include(model).value()
@_remove(model)
+ # FIXME: thish should trigger on the previous method
+ @trigger("remove", model)
def reFilter: ->
@parent.each (model) => @determinePlacementInSelf(model)
@@ -1,6 +1,7 @@
AS = require "alpha_simprini"
AS.Models.Group = AS.Model.extend ({delegate, include, def, defs}) ->
+ @property "metaData"
@field "name"
@hasMany "members"
@@ -2,7 +2,7 @@ AS = require "alpha_simprini"
AS.Models.Grouping = AS.Model.extend ({delegate, include, def, defs}) ->
@hasMany 'groups'
- def initialize: (@backingCollection, @groupByProperty) ->
+ def initialize: (@backingCollection, @groupByProperty, @metaData={}) ->
@_super()
@groupMap = AS.Map.new()
# TODO: send newvalue/oldvalue when triggering field changes ;)
@@ -29,7 +29,7 @@ AS.Models.Grouping = AS.Model.extend ({delegate, include, def, defs}) ->
def addToGroup: (item) ->
name = item[@groupByProperty].get()
unless group = @groupMap.get(name)
- group = AS.Models.Group.new(name: name)
+ group = AS.Models.Group.new(name: name, metaData: @metaData)
@groups.add(group)
@groupMap.set(name, group)
@@ -15,6 +15,8 @@ AS.Model.VirtualProperty.Instance = AS.Property.Instance.extend ({def}) ->
@cached = NULL_CACHE
for dependency in @options.dependencies
@object.bind "change:#{dependency}", _.bind @triggerFor, this, dependency
+ @object[dependency].bind "add", _.bind @triggerFor, this, dependency
+ @object[dependency].bind "remove", _.bind @triggerFor, this, dependency
def set: (value) ->
if set = @options.getSet.set
@@ -6,7 +6,8 @@ exports.setUp = coreSetUp
NS.Virtualized = AS.Model.extend ->
@field "name"
@property "other"
- @virtualProperties "name",
+ @hasMany "others"
+ @virtualProperties "name", "others",
virtualA: -> Math.random()
virtualB: -> "steady as she goes"
virtualC:
@@ -20,7 +21,7 @@ exports.VirtualProperty =
test.done()
"exposes dependencies": (test) ->
- test.deepEqual NS.Virtualized.properties.virtualA.dependencies, ["name"]
+ test.deepEqual NS.Virtualized.properties.virtualA.dependencies, ["name", "others"]
test.done()
"when dependency and virtual changes, change triggers on virtual": (test) ->
@@ -42,6 +43,15 @@ exports.VirtualProperty =
test.equal "vname", o.name.get()
test.done()
+ "may depend on collection properties": (test) ->
+ test.expect 3
+ o = NS.Virtualized.new()
+ o.bind "change:virtualA", -> test.ok true
+ o.bind "change:virtualB", -> test.ok true
+ o.bind "change:virtualC", -> test.ok true
+ o.others.add {}
+ test.done()
+
"bindPath":
"may be used in path bindings": (test) ->
o = NS.Virtualized.new()

0 comments on commit d3b7f84

Please sign in to comment.