Created
March 2, 2012 06:16
-
-
Save wycats/1956171 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if (typeof Proxy !== undefined) { | |
var createProxy = function(object) { | |
var proxy = Proxy.create({ | |
get: function(receiver, name) { | |
if (name === '__proto__') { return object.__proto__; } | |
return Ember.get(object, name); | |
}, | |
set: function(receiver, name, value) { | |
return Ember.set(object, name, value); | |
}, | |
getOwnPropertyDescriptor: function(name) { | |
var descs = Ember.meta(object).descs; | |
// TODO: Distinguish between reopen and subclassing | |
var desc = descs[name]; | |
if (desc instanceof Ember.Descriptor) { | |
return { | |
value: Ember.get(object, name), | |
writable: true, | |
configurable: true, | |
enumerable: true | |
} | |
} else { | |
return Object.getOwnPropertyDescriptor(object, name); | |
} | |
}, | |
getPropertyDescriptor: function(name) { | |
return this.getOwnPropertyDescriptor(name); | |
}, | |
getOwnPropertyNames: function() { | |
return Object.getOwnPropertyNames(object); | |
}, | |
getPropertyNames: function() { | |
var names = []; | |
for (var prop in object) { | |
names.push(prop); | |
} | |
return names; | |
}, | |
defineProperty: function(name, descriptor) { | |
Ember.defineProperty(object, name, descriptor); | |
return Ember.meta(object).descs[name] | |
}, | |
"delete": function(name) { | |
// TODO: Clean up | |
return delete object[name]; | |
} | |
// fix not implemented | |
}, object.constructor.prototype); | |
object.proxy = proxy; | |
return proxy; | |
}; | |
x = Ember.Object.create({ | |
foo: Ember.computed(function() { | |
return "foo"; | |
}) | |
}); | |
p = createProxy(x); | |
console.log(p); | |
module("Using proxies"); | |
test("proxies work on basic properties", function() { | |
var object = createProxy(Ember.Object.create()); | |
object.x = 1; | |
equal(object.x, 1, "basic properties work"); | |
}); | |
test("proxies work for computed properties", function() { | |
var count = 0, cached = 0; | |
var object = createProxy(Ember.Object.create({ | |
computed: Ember.computed(function() { | |
return "computed " + (++count); | |
}), | |
cacheable: Ember.computed(function() { | |
return "computed " + (++cached); | |
}).cacheable() | |
})); | |
equal(object.computed, "computed 1"); | |
equal(object.computed, "computed 2"); | |
equal(object.computed, "computed 3"); | |
equal(object.cacheable, "computed 1"); | |
equal(object.cacheable, "computed 1"); | |
equal(object.cacheable, "computed 1"); | |
}); | |
test("proxies work for settable computed properties", function() { | |
var object = createProxy(Ember.Object.create({ | |
raw: null, | |
array: Ember.computed(function(key, value) { | |
if (arguments.length === 2) { | |
this.raw = value.join(","); | |
} else { | |
return this.raw.split(","); | |
} | |
}).property("raw") | |
})); | |
object.array = [ "one", "two", "three" ]; | |
deepEqual(object.array, [ "one", "two", "three" ]); | |
}); | |
test("proxies support functions with super", function() { | |
var Klass = Ember.Object.extend({ | |
parent: function() { | |
return "parent"; | |
} | |
}); | |
var obj = createProxy(Klass.create({ | |
parent: function() { | |
return this._super() + " child"; | |
} | |
})); | |
equal(obj.parent(), "parent child"); | |
}); | |
test("proxies support unknownProperty and setUnknownProperty", function() { | |
var count = 0; | |
var object = createProxy(Ember.Object.create({ | |
meta: {}, | |
regular: "regular", | |
unknownProperty: function(key) { | |
return this.meta[key]; | |
}, | |
setUnknownProperty: function(key, value) { | |
this.meta[key] = value; | |
} | |
})); | |
equal(object.foo, undefined); | |
object.foo = 1; | |
equal(object.foo, 1); | |
equal(object.meta.foo, 1); | |
object.foo = 2; | |
equal(object.foo, 2); | |
equal(object.meta.foo, 2); | |
equal(object.regular, "regular"); | |
object.regular = "other"; | |
equal(object.meta.regular, undefined); | |
equal(object.regular, "other"); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment