Permalink
Browse files

fixed bugs with virtuals and collection inversion

  • Loading branch information...
1 parent 217441a commit 8185c58f8f0fa40a0230443eb356efab4cc07232 @collin committed Apr 5, 2012
@@ -24,8 +24,7 @@ AS.Property = Taxi.Property
AS.COLLECTION_DELEGATES = ["first", "rest", "last", "compact", "flatten", "without", "union", "filter", "reverse",
"intersection", "difference", "uniq", "zip", "indexOf", "find", "detect",
- "lastIndexOf", "range", "include", "each", "map", "reject","all", "toArray"]
-
+ "lastIndexOf", "range", "include", "each", "map", "reject","all", "toArray", "pluck"]
AS.require = (framework="alpha_simprini", libraries) ->
if libraries is undefined
@@ -66,7 +66,7 @@ AS.Collection = AS.Object.extend ({def, include, delegate}) ->
model = model.model
result = @_remove(model, options)
- model[@inverse](null) if @inverse
+ model[@inverse].set(null) if @inverse
result
@@ -2,27 +2,29 @@ AS = require("alpha_simprini")
_ = require("underscore")
{isBoolean} = require "underscore"
-casters =
- String:
- read: String
- write: String
- Number:
- read: Number
- write: Number
- Boolean:
- read: (value) ->
- return value if isBoolean(value)
- return true if value is "true"
- return false if value is "false"
- return false
-
- write: (value) ->
- return "true" if value is "true" or value is true
- return "false" if value is "false" or value is false
- return "false"
-
# TODO: Field is generic. reuse it.
-AS.Model.Field = AS.Property.extend ({def}) ->
+AS.Model.Field = AS.Property.extend ({delegate, include, def, defs}) ->
+ defs Casters:
+ String:
+ read: String
+ write: String
+ Number:
+ read: Number
+ write: Number
+ Boolean:
+ read: (value) ->
+ return value if isBoolean(value)
+ return true if value is "true"
+ return false if value is "false"
+ return false
+
+ write: (value) ->
+ return "true" if value is "true" or value is true
+ return "false" if value is "false" or value is false
+ return "false"
+
+ Casters = @Casters
+
def initialize: (@name, @_constructor, @options={}) ->
@options.name = @name
@_constructor.writeInheritableValue 'properties', @name, this
@@ -48,12 +50,12 @@ AS.Model.Field = AS.Property.extend ({def}) ->
def get: ->
if @value isnt undefined
- value = casters[@options.type.name].read(@value)
+ value = Casters[@options.type.name].read(@value)
else
@options.default
def set: (value) ->
- writeValue = casters[@options.type.name].write(value)
+ writeValue = Casters[@options.type.name].write(value)
return if writeValue is @value
@value = writeValue
@object.trigger("change")
@@ -24,6 +24,9 @@ AS.Model.HasMany.Instance = AS.Model.Field.Instance.extend ({def, delegate}) ->
def unbind: -> @backingCollection.unbind.apply(@backingCollection, arguments)
+ def pluck: (key) ->
+ @map (item) -> item[key].get()
+
def any: ->
_.any @backingCollection
@@ -4,6 +4,7 @@ _ = require "underscore"
AS.Model.VirtualProperty = AS.Property.extend ({def}) ->
def initialize: (@name, @_constructor, @options={}) ->
@options.name = @name
+ @dependencies = @options.dependencies
@_constructor.writeInheritableValue 'properties', @name, this
def instance: (object) -> @constructor.Instance.new(object, @options)
@@ -17,7 +18,7 @@ AS.Model.VirtualProperty.Instance = AS.Property.Instance.extend ({def}) ->
def set: () -> throw "Can't set a VirtualProperty name: #{@options.name}, dependencies: #{@options.dependencies.join(',')}"
- def get: -> @cached ?= @compute()
+ def get: -> @cached = @compute()
def compute: (args) -> @options.fn.call(@object)
@@ -18,6 +18,20 @@ exports.Collection =
test.equal "SOURCE", things.first().value().inverse.get()
test.done()
+ "clears inverse if specified": (test) ->
+ C.Thing = AS.Model.extend()
+ C.Thing.property("inverse")
+ C.ThingCollection = AS.Collection.extend ->
+ @def model: -> C.Thing
+ @def inverse: "inverse"
+
+ things = C.ThingCollection.new()
+ things.source = "SOURCE"
+ thing = things.add()
+ things.remove(thing)
+ test.equal null, thing.inverse.get()
+ test.done()
+
"inserts item of specified type": (test) ->
C.Thing = AS.Model.extend()
C.ThingCollection = AS.Collection.extend -> @def model: -> C.Thing
@@ -16,7 +16,7 @@ exports.VirtualProperty =
test.done()
"exposes dependencies": (test) ->
- test.deepEqual NS.Virtualized.properties.virtualA.options.dependencies, ["name"]
+ test.deepEqual NS.Virtualized.properties.virtualA.dependencies, ["name"]
test.done()
"when dependency and virtual changes, change triggers on virtual": (test) ->

0 comments on commit 8185c58

Please sign in to comment.