Skip to content

Commit

Permalink
fix reworked setter
Browse files Browse the repository at this point in the history
  • Loading branch information
sorenbs committed Apr 14, 2011
1 parent fa9e2e1 commit d35170a
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 3 deletions.
5 changes: 3 additions & 2 deletions src/core.js
Expand Up @@ -304,9 +304,9 @@ Crafty.fn = Crafty.prototype = {

setter: function(prop, fn) {
if(Crafty.support.setter) {
this.__defineSetter__(property, fn);
this.__defineSetter__(prop, fn);
} else if(Crafty.support.defineProperty) {
Object.defineProperty(this, property, {
Object.defineProperty(this, prop, {
set: fn,
configurable : true
});
Expand All @@ -317,6 +317,7 @@ Crafty.fn = Crafty.prototype = {
fn: fn
});
}
return this;
},

destroy: function() {
Expand Down
2 changes: 1 addition & 1 deletion tests/core.html
@@ -1 +1 @@
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head> <script src="http://code.jquery.com/jquery-latest.js"></script> <link rel="stylesheet" href="http://code.jquery.com/qunit/git/qunit.css" type="text/css" media="screen" /> <script type="text/javascript" src="http://code.jquery.com/qunit/git/qunit.js"></script> <script type="text/javascript" src="../crafty.js"></script><script>$(document).ready(function() { var first = Crafty.e("test"); Crafty.e("test"); Crafty.e("test"); Crafty.e("test"); Crafty.e("test2"); Crafty.e("test2"); Crafty.e("test2"); Crafty.e("test2"); Crafty.e("test, test2"); Crafty.e("test, test2"); Crafty.e("test, test2"); Crafty.e("test, test2"); Crafty.c("comp", { added: true }); module("CORE"); test("selectors", function() { equals( Crafty("test").length, 8, "Single component" ); equals( Crafty("test test2").length, 4, "Two components ANDed" ); equals( Crafty("test, test2").length, 12, "Two components ORed" ); equals( Crafty("*").length, 13, "All components - universal selector" ); equals( Crafty(1), first, "Get by ID" ); }); test("addComponent", function() { first.addComponent("test3"); equals( first.has("test3"), true, "component added" ); first.addComponent("comp"); equals( first.added, true, "component with property exists" ); first.addComponent("multi1, multi2"); equals( first.has("multi1") && first.has("multi2"), true, "multiple components added" ); }); test("removeComponent", function() { first.removeComponent("test3"); equals( first.has("test3"), false, "component removed"); first.removeComponent("comp"); equals( first.added && !first.has("comp"), true, "component not haz but properties remain" ); }); test("attr", function() { first.attr("single", true); equals( first.single, true, "single attribute assigned" ); first.attr({prop: "test", another: 56}); equals( first.prop, "test", "properties from object assigned" ); equals( first.another, 56, "properties from object assigned" ); }); test("getterSetter", function() { first.getter('p1', function() {return 5 }); equals(first.p1, 5, "single property getter"); first.getter('p2', function() {return this._p2}).setter('p2', function(v) {this._p2 = v*2}); first.p2 = 13; Crafty.trigger("enterframe"); //IE<9 implementation does its thing at enterframe events. equals(first.p2, first._p2, "single property setter"); first.getter('p3', function() {return this._p3/3}).setter('p3', function(v) {this._p3 = v*3}) .getter('p4', function() {return this._p4}).setter('p4', function(v) {this._p4 = v*4}); first.p3 = 9; first.p4 = 3; Crafty.trigger("enterframe"); equals(first.p3, 9, "multiple property getter/setter"); equals(first.p4, 12, "multiple property getter/setter"); first.bind("change", function() { first.changeRegistered = true; }); first.p4 = 42; Crafty.trigger("enterframe"); equals(first.changeRegistered, true, "change event triggered") first.unbind("change"); }); test("bind", function() { first.bind("myevent", function() { ok(true, "Custom event"); }); }); test("trigger", function() { first.trigger("myevent"); }); test("unbind", function() { first.bind("myevent", function() { ok(false, "This should not be triggered (unbinding all)"); }); first.unbind("myevent"); first.trigger("myevent"); function callback() { ok(false, "This should also not be triggered (unbind by FN)"); } function callback2() { ok(true, "This should be triggered"); } first.bind("myevent", callback); first.bind("myevent", callback2); first.unbind("myevent", callback); first.trigger("myevent"); }); test("each", function() { var count = 0; Crafty("test").each(function(i) { count++; }); equals(count, 8, "Iterated all elements"); }); test("requires", function() { Crafty.c("already", { already: true }); Crafty.c("notyet", { notyet: true }); first.addComponent("already"); first.already = "already"; first.requires("already, notyet"); equals(first.already, "already", "Didn't overwrite property"); equals(first.notyet, true, "Assigned if didn't have"); ok(first.has("already") && first.has("notyet"), "Both added"); }); test("destroy", function() { var id = first[0]; //id first.destroy(); equals(Crafty(id).length, 0, "Not listed"); }); module("2D"); Crafty.init(); var player = Crafty.e("2D, DOM, color").attr({w:50, h: 50}).color("red"); test("position", function() { player.x += 50; equals(player._x, 50, "X moved"); player.y += 50; equals(player._y, 50, "Y moved"); player.w += 50; equals(player._w, 100, "Width increase"); player.h += 50; equals(player._h, 100, "Height increase"); equals(player._global, 13, "Global Z, Before"); player.z = 1; equals(player._z, 1, "Z index"); equals(player._global, 100013, "Global Z, After"); }); test("intersect", function() { player.x = 0; player.y = 0; player.w = 50; player.h = 50; equals(player.intersect(0, 0, 100, 50), true, "Intersected"); equals(player.intersect({x: 0, y: 0, w: 100, h: 50}), true, "Intersected Again"); equals(player.intersect(100, 100, 100, 50), false, "Didn't intersect"); }); });</script> </head><body><h1 id="qunit-header">Crafty: Core</h1><h2 id="qunit-banner"></h2><div id="qunit-testrunner-toolbar"></div><h2 id="qunit-userAgent"></h2><ol id="qunit-tests"></ol><div id="qunit-fixture">test markup, will be hidden</div></body></html>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head> <script src="http://code.jquery.com/jquery-latest.js"></script> <link rel="stylesheet" href="http://code.jquery.com/qunit/git/qunit.css" type="text/css" media="screen" /> <script type="text/javascript" src="http://code.jquery.com/qunit/git/qunit.js"></script> <script type="text/javascript" src="../crafty.js"></script><script>$(document).ready(function() { var first = Crafty.e("test"); Crafty.e("test"); Crafty.e("test"); Crafty.e("test"); Crafty.e("test2"); Crafty.e("test2"); Crafty.e("test2"); Crafty.e("test2"); Crafty.e("test, test2"); Crafty.e("test, test2"); Crafty.e("test, test2"); Crafty.e("test, test2"); Crafty.c("comp", { added: true }); module("CORE"); test("selectors", function() { equals( Crafty("test").length, 8, "Single component" ); equals( Crafty("test test2").length, 4, "Two components ANDed" ); equals( Crafty("test, test2").length, 12, "Two components ORed" ); equals( Crafty("*").length, 13, "All components - universal selector" ); equals( Crafty(1), first, "Get by ID" ); }); test("addComponent", function() { first.addComponent("test3"); equals( first.has("test3"), true, "component added" ); first.addComponent("comp"); equals( first.added, true, "component with property exists" ); first.addComponent("multi1, multi2"); equals( first.has("multi1") && first.has("multi2"), true, "multiple components added" ); }); test("removeComponent", function() { first.removeComponent("test3"); equals( first.has("test3"), false, "component removed"); first.removeComponent("comp"); equals( first.added && !first.has("comp"), true, "component not haz but properties remain" ); }); test("attr", function() { first.attr("single", true); equals( first.single, true, "single attribute assigned" ); first.attr({prop: "test", another: 56}); equals( first.prop, "test", "properties from object assigned" ); equals( first.another, 56, "properties from object assigned" ); }); test("getterSetter", function() { first.setter('p1', function(v) {this._p1 = v*2 }); first.p1 = 2; equals(first._p1, 4, "single property getter"); first.getter('p1', function() {return 5 }); equals(first.p1, 5, "single property getter"); first.getter('p2', function() {return this._p2}).setter('p2', function(v) {this._p2 = v*2}); first.p2 = 13; Crafty.trigger("enterframe"); //IE<9 implementation does its thing at enterframe events. equals(first.p2, first._p2, "single property setter"); first.getter('p3', function() {return this._p3/3}).setter('p3', function(v) {this._p3 = v*3}) .getter('p4', function() {return this._p4}).setter('p4', function(v) {this._p4 = v*4}); first.p3 = 9; first.p4 = 3; Crafty.trigger("enterframe"); equals(first.p3, 9, "multiple property getter/setter"); equals(first.p4, 12, "multiple property getter/setter"); first.bind("change", function() { first.changeRegistered = true; }); first.p4 = 42; Crafty.trigger("enterframe"); equals(first.changeRegistered, true, "change event triggered") first.unbind("change"); }); test("bind", function() { first.bind("myevent", function() { ok(true, "Custom event"); }); }); test("trigger", function() { first.trigger("myevent"); }); test("unbind", function() { first.bind("myevent", function() { ok(false, "This should not be triggered (unbinding all)"); }); first.unbind("myevent"); first.trigger("myevent"); function callback() { ok(false, "This should also not be triggered (unbind by FN)"); } function callback2() { ok(true, "This should be triggered"); } first.bind("myevent", callback); first.bind("myevent", callback2); first.unbind("myevent", callback); first.trigger("myevent"); }); test("each", function() { var count = 0; Crafty("test").each(function(i) { count++; }); equals(count, 8, "Iterated all elements"); }); test("requires", function() { Crafty.c("already", { already: true }); Crafty.c("notyet", { notyet: true }); first.addComponent("already"); first.already = "already"; first.requires("already, notyet"); equals(first.already, "already", "Didn't overwrite property"); equals(first.notyet, true, "Assigned if didn't have"); ok(first.has("already") && first.has("notyet"), "Both added"); }); test("destroy", function() { var id = first[0]; //id first.destroy(); equals(Crafty(id).length, 0, "Not listed"); }); module("2D"); Crafty.init(); var player = Crafty.e("2D, DOM, color").attr({w:50, h: 50}).color("red"); test("position", function() { player.x += 50; equals(player._x, 50, "X moved"); player.y += 50; equals(player._y, 50, "Y moved"); player.w += 50; equals(player._w, 100, "Width increase"); player.h += 50; equals(player._h, 100, "Height increase"); equals(player._global, 13, "Global Z, Before"); player.z = 1; equals(player._z, 1, "Z index"); equals(player._global, 100013, "Global Z, After"); }); test("intersect", function() { player.x = 0; player.y = 0; player.w = 50; player.h = 50; equals(player.intersect(0, 0, 100, 50), true, "Intersected"); equals(player.intersect({x: 0, y: 0, w: 100, h: 50}), true, "Intersected Again"); equals(player.intersect(100, 100, 100, 50), false, "Didn't intersect"); }); });</script> </head><body><h1 id="qunit-header">Crafty: Core</h1><h2 id="qunit-banner"></h2><div id="qunit-testrunner-toolbar"></div><h2 id="qunit-userAgent"></h2><ol id="qunit-tests"></ol><div id="qunit-fixture">test markup, will be hidden</div></body></html>
Expand Down

0 comments on commit d35170a

Please sign in to comment.