Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

allow specifying observers and computed properties during resource cr…

…eations. fixes #22
  • Loading branch information...
commit 7187ee89105d53d69f429cf0729ae20e8de0478b 1 parent f3f8186
@staugaard staugaard authored
Showing with 52 additions and 2 deletions.
  1. +34 −1 spec/javascripts/resourceSpec.js
  2. +18 −1 src/sproutcore-resource.js
View
35 spec/javascripts/resourceSpec.js
@@ -45,10 +45,43 @@ describe('A Resource instance', function() {
});
it('allows setting of properties not in the schema during creation', function() {
- model = Model.create({ undefinedProperty: 'foo' });
+ model = Model.create({
+ undefinedProperty: 'foo'
+ });
+
expect(model.get('undefinedProperty')).toEqual('foo');
});
+ it('allows setting functions during creation', function() {
+ model = Model.create({
+ undefinedProperty: function() { return 'foo'; }
+ });
+
+ expect(SC.typeOf(model.undefinedProperty)).toEqual('function');
+ expect(model.undefinedProperty()).toEqual('foo');
+ });
+
+ it('allows setting observers during creation', function() {
+ var observerDidFire = false;
+ model = Model.create({
+ myObserver: function() { observerDidFire = true; }.observes('foo')
+ });
+ observerDidFire = false;
+ model.set('foo', 'new value');
+
+ expect(observerDidFire).toBe(true);
+ });
+
+ it('allows setting computed properties during creation', function() {
+ model = Model.create({
+ undefinedProperty: function() { return this.get('foo') + '!'; }.property('foo')
+ });
+ model.set('foo', 'foo');
+
+ expect(model.get('undefinedProperty')).toEqual('foo!');
+ });
+
+
it('allows setting of properties not in the schema during creation, considering paths', function() {
Model = SC.Resource.define({
View
19 src/sproutcore-resource.js
@@ -935,7 +935,24 @@
}
delete options.data;
- instance.setProperties(options);
+
+ SC.beginPropertyChanges(this);
+ var mixin = {};
+ var hasMixin = false;
+ for (var name in options) {
+ if (options.hasOwnProperty(name)) {
+ if (this.schema[name]) {
+ instance.set(name, options[name]);
+ } else {
+ mixin[name] = options[name];
+ hasMixin = true;
+ }
+ }
+ }
+ if (hasMixin) {
+ instance.reopen(mixin);
+ }
+ SC.endPropertyChanges(this);
return instance;
} else {
Please sign in to comment.
Something went wrong with that request. Please try again.