Permalink
Browse files

Added jquery dom selector plugin, mimics other dom.query plugins. Pro…

…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...
1 parent ed59241 commit e9fa60a20ac829f65bd53bc5e60ca3212d8aca21 @briancavalier briancavalier committed Nov 8, 2011
Showing with 148 additions and 11 deletions.
  1. +3 −0 test/all.js
  2. +1 −1 test/curl
  3. +56 −0 test/jquery/dom-query.html
  4. +25 −1 test/prototype1.html
  5. +1 −1 wire.js
  6. +14 −8 wire/base.js
  7. +48 −0 wire/jquery/dom.js
View
@@ -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');
View
@@ -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>
View
@@ -18,6 +18,7 @@
//
require(['wire'], function(wire) {
wire({
+ debug: {module:'wire/debug'},
// A simple example using object literals
//
@@ -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
@@ -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);
View
@@ -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";
View
@@ -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)
);
}
@@ -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);
});
@@ -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();
}
View
@@ -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.