Skip to content

Commit

Permalink
Added jquery dom selector plugin, mimics other dom.query plugins. Pro…
Browse files Browse the repository at this point in the history
…totype factory can now take any wireable as the prototype--e.g. any ref, object literal, etc. but also retains backward compatibility with passing just a refName string. Automated unit tests for prototype factory.
  • Loading branch information
briancavalier committed Nov 8, 2011
1 parent ed59241 commit e9fa60a
Show file tree
Hide file tree
Showing 7 changed files with 148 additions and 11 deletions.
3 changes: 3 additions & 0 deletions test/all.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ doh.registerUrl('destroy-facet', '../../destroy.html');
// literal
doh.registerUrl('literal-factory', '../../literal.html');

// prototype
doh.registerUrl('prototype-factory', '../../prototype1.html');

// wire/dom
doh.registerUrl('dom-resolver', '../../dom1.html');

Expand Down
56 changes: 56 additions & 0 deletions test/jquery/dom-query.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<!DOCTYPE HTML>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<title>wire/jquery/dom test</title>

<script src="../util/doh/runner.js"></script>
<script src="../test-config.js"></script>

<script type="text/javascript">
require(['wire'], function(wire) {
wire({
plugins: [
// { module: 'wire/debug' },
{ module: 'wire/jquery/dom' }
],
node1: { $ref: 'dom.query!#node1' },
divs: { $ref: 'dom.query!.test' },
div: { $ref: 'dom.query!.test', i: 0 }
}).then(
function(context) {
doh.register('jquery/dom', [
function(doh) {
// Sizzle always returns an array, so test
// against the first
doh.assertEqual(1, context.node1.length);
doh.assertEqual('node1', context.node1[0].id);
},
function(doh) {
doh.assertEqual(3, context.divs.length);
},
function(doh) {
// Using the plugin's i option to extract a single
// node
doh.assertEqual('test one', context.div.className);
}
]);

doh.run();
},
function(err) {
console.log(err);
}
);
});
</script>
</head>
<body>
<div>
<p id="node1"></p>
</div>
<div class="test one"></div>
<div class="test two"></div>
<div class="test three"></div>
</body>
</html>
26 changes: 25 additions & 1 deletion test/prototype1.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
//
require(['wire'], function(wire) {
wire({
debug: {module:'wire/debug'},
// A simple example using object literals

//
Expand Down Expand Up @@ -64,6 +65,17 @@
ready: "Hi Mom"
}
},

parent3: {
a: 'a'
},
child3: {
prototype: { $ref: 'parent3' },
properties: {
a: 'a-child',
b: 'b-child'
}
},

//
// Create a logger for use above
Expand All @@ -72,7 +84,19 @@
}
}).then(
function(context) {
console.log("DONE", context);
var undef;

doh.register('prototype-factory', [
function testPrototypeWithRef(doh) {
doh.assertEqual('a', context.parent3.a);
doh.assertEqual(undef, context.parent3.b);

doh.assertEqual('a-child', context.child3.a);
doh.assertEqual('b-child', context.child3.b);
}
]);

doh.run();
},
function(err) {
console.error(err);
Expand Down
2 changes: 1 addition & 1 deletion wire.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

//noinspection ThisExpressionReferencesGlobalObjectJS
(function(global, define){
define(['require', 'when', 'wire/base'], function(require, when, basePlugin) {
define(['require', 'when', 'wire/base'], function(require, when, basePlugin) {

"use strict";

Expand Down
22 changes: 14 additions & 8 deletions wire/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,15 +112,21 @@ define(['when'], function(when) {
promise.resolve(spec.literal);
}

function protoFactory(promise, spec, wire) {
var parentRef = spec.prototype;

wire.resolveRef(parentRef).then(
function protoFactory(resolver, spec, wire) {
var parentRef, promise;

parentRef = spec.prototype;

promise = typeof parentRef === 'string'
? wire.resolveRef(parentRef)
: wire(parentRef);

when(promise,
function(parent) {
var child = createObject(parent);
promise.resolve(child);
resolver.resolve(child);
},
reject(promise)
reject(resolver)
);
}

Expand All @@ -139,7 +145,7 @@ define(['when'], function(when) {
function setProperty(proxy, name, val, wire) {
var promise = wire(val, name, proxy.path);

promise.then(function(resolvedValue) {
when(promise, function(resolvedValue) {
proxy.set(name, resolvedValue);
});

Expand Down Expand Up @@ -177,7 +183,7 @@ define(['when'], function(when) {
// when this context is destroyed
var destroyFuncs = [];

destroyed.then(function() {
when(destroyed, function() {
for(var i = 0, destroy; (destroy = destroyFuncs[i++]);) {
destroy();
}
Expand Down
48 changes: 48 additions & 0 deletions wire/jquery/dom.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
* @license Copyright (c) 2010-2011 Brian Cavalier
* LICENSE: see the LICENSE.txt file. If file is missing, this file is subject
* to the MIT License at: http://www.opensource.org/licenses/mit-license.php.
*/

/**
* dom.js
*/
define(['js!jquery-1.7.min.js', 'wire/domReady'], function(jquery, domReady) {

function resolveQuery(resolver, name, refObj /*, wire */) {

domReady(function() {
var result, i;

result = jQuery(name);
i = refObj.i;

if (typeof i == 'number') {
if (i < result.length) {
resolver.resolve(result[i]);
} else {
resolver.reject(new Error("Query '" + name + "' returned " + result.length + " items while expecting at least " + (refObj.i + 1)));
}
} else {
resolver.resolve(jQuery.makeArray(result));
}
});

}

/**
* The plugin instance. Can be the same for all wiring runs
*/
var plugin = {
resolvers: {
'dom.query': resolveQuery
}
};

return {
wire$plugin: function(/*ready, destroyed, options*/) {
return plugin;
}
};

});

0 comments on commit e9fa60a

Please sign in to comment.