Skip to content

Commit

Permalink
created a way to init the code without autobootstrap
Browse files Browse the repository at this point in the history
  • Loading branch information
Misko Hevery committed Jan 9, 2010
1 parent fac0e69 commit 214c142
Show file tree
Hide file tree
Showing 19 changed files with 181 additions and 176 deletions.
10 changes: 10 additions & 0 deletions .externalToolBuilders/JSTD_Tests.launch
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<launchConfiguration type="org.eclipse.ui.externaltools.ProgramBuilderLaunchConfigurationType">
<booleanAttribute key="org.eclipse.debug.ui.ATTR_LAUNCH_IN_BACKGROUND" value="false"/>
<stringAttribute key="org.eclipse.ui.externaltools.ATTR_BUILD_SCOPE" value="${working_set:&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;&#10;&lt;launchConfigurationWorkingSet editPageId=&quot;org.eclipse.ui.resourceWorkingSetPage&quot; factoryID=&quot;org.eclipse.ui.internal.WorkingSetFactory&quot; id=&quot;1262905463390_2&quot; label=&quot;workingSet&quot; name=&quot;workingSet&quot;&gt;&#10;&lt;item factoryID=&quot;org.eclipse.ui.internal.model.ResourceFactory&quot; path=&quot;/angular.js/src&quot; type=&quot;2&quot;/&gt;&#10;&lt;item factoryID=&quot;org.eclipse.ui.internal.model.ResourceFactory&quot; path=&quot;/angular.js/test&quot; type=&quot;2&quot;/&gt;&#10;&lt;/launchConfigurationWorkingSet&gt;}"/>
<stringAttribute key="org.eclipse.ui.externaltools.ATTR_LOCATION" value="/usr/bin/java"/>
<stringAttribute key="org.eclipse.ui.externaltools.ATTR_RUN_BUILD_KINDS" value="full,incremental,auto,"/>
<stringAttribute key="org.eclipse.ui.externaltools.ATTR_TOOL_ARGUMENTS" value="-jar lib/jstestdriver/JsTestDriver.jar --tests all"/>
<booleanAttribute key="org.eclipse.ui.externaltools.ATTR_TRIGGERS_CONFIGURED" value="true"/>
<stringAttribute key="org.eclipse.ui.externaltools.ATTR_WORKING_DIRECTORY" value="${workspace_loc:/angular.js}"/>
</launchConfiguration>
27 changes: 27 additions & 0 deletions .project
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>angular.js</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.wst.jsdt.core.javascriptValidator</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.ui.externaltools.ExternalToolBuilder</name>
<triggers>auto,full,incremental,</triggers>
<arguments>
<dictionary>
<key>LaunchConfigHandle</key>
<value>&lt;project&gt;/.externalToolBuilders/JSTD_Tests.launch</value>
</dictionary>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.wst.jsdt.core.jsNature</nature>
</natures>
</projectDescription>
13 changes: 13 additions & 0 deletions .settings/.jsdtscope
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry excluding="lib/swfobject/|test/test/|src/test/|src/|lib/jquery/|lib/webtoolkit/|lib/underscore/|test/" kind="src" path=""/>
<classpathentry kind="src" path="lib/jquery"/>
<classpathentry kind="src" path="lib/swfobject"/>
<classpathentry kind="src" path="lib/underscore"/>
<classpathentry kind="src" path="lib/webtoolkit"/>
<classpathentry excluding="test/" kind="src" path="src"/>
<classpathentry kind="src" path="src/test"/>
<classpathentry excluding="test/" kind="src" path="test"/>
<classpathentry kind="src" path="test/test"/>
<classpathentry kind="con" path="org.eclipse.wst.jsdt.launching.JRE_CONTAINER"/>
</classpath>
1 change: 1 addition & 0 deletions .settings/org.eclipse.wst.jsdt.ui.superType.container
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
org.eclipse.wst.jsdt.launching.JRE_CONTAINER
1 change: 1 addition & 0 deletions .settings/org.eclipse.wst.jsdt.ui.superType.name
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Global
128 changes: 0 additions & 128 deletions angular.js

This file was deleted.

12 changes: 12 additions & 0 deletions example/calculator-auto_init.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<script type="text/javascript" src="../src/angular-bootstrap.js"></script>
</head>
<body>
Quantity: <input type="text" name="a" value="2">
*
Cost: <input type="text" name="b" value="3.4">
= {{a * b | currency}}
</body>
</html>
20 changes: 20 additions & 0 deletions example/calculator-manual_init.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<script type="text/javascript" src="../src/angular-bootstrap.js#autoBind=false"></script>
<script type="text/javascript">
$(document).ready(function(){
var angular = angularFactory({});
var scope = angular.compile(document);
scope.set('a', 3);
scope.updateView();
});
</script>
</head>
<body>
Quantity: <input type="text" name="a" value="2">
*
Cost: <input type="text" name="b" value="3.4">
= {{a * b | currency}}
</body>
</html>
11 changes: 11 additions & 0 deletions example/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
</head>
<body>
<ul>
<li><a href="calculator-auto_init.html">Calculator: Auto Init</a></li>
<li><a href="calculator-manual_init.html">Calculator: Manual Init</a></li>
</ul>
</body>
</html>
5 changes: 3 additions & 2 deletions jsTestDriver.conf
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
server: http://localhost:9876

load:
- lib/swfobject/swfobject.js
- lib/webtoolkit/webtoolkit.base64.js
- lib/jquery/jquery-1.3.2.js
- lib/jquery/jquery-ui-1.7.1.custom.min.js
Expand All @@ -13,4 +12,6 @@ load:
- test/testabilityPatch.js
- test/test/*.js
- test/*.js


exclude:
- src/angular-bootstrap.js
1 change: 1 addition & 0 deletions server.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
java -jar lib/jstestdriver/JsTestDriver.jar --port 9876
17 changes: 13 additions & 4 deletions src/Binder.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,17 @@ nglr.Binder.prototype.updateView = function() {
_.each(this.updateListeners, function(fn) {fn();});
};

nglr.Binder.prototype.docFindWithSelf = function(exp){
var doc = jQuery(this.doc);
var selection = doc.find(exp);
if (doc.is(exp)){
selection = selection.andSelf();
}
return selection;
};

nglr.Binder.prototype.executeInit = function() {
jQuery("[ng-init]", this.doc).each(function() {
this.docFindWithSelf("[ng-init]").each(function() {
var jThis = jQuery(this);
var scope = jThis.scope();
try {
Expand All @@ -116,7 +125,7 @@ nglr.Binder.prototype.executeInit = function() {
};

nglr.Binder.prototype.entity = function (scope) {
jQuery("[ng-entity]", this.doc).attr("ng-watch", function() {
this.docFindWithSelf("[ng-entity]").attr("ng-watch", function() {
try {
var jNode = jQuery(this);
var decl = scope.entity(jNode.attr("ng-entity"));
Expand All @@ -131,12 +140,12 @@ nglr.Binder.prototype.compile = function() {
var jNode = jQuery(this.doc);
var self = this;
if (this.config.autoSubmit) {
var submits = jQuery(":submit", this.doc).not("[ng-action]");
var submits = this.docFindWithSelf(":submit").not("[ng-action]");
submits.attr("ng-action", "$save()");
submits.not(":disabled").not("ng-bind-attr").attr("ng-bind-attr", '{disabled:"{{$invalidWidgets}}"}');
}
this.precompile(this.doc)(this.doc, jNode.scope(), "");
jQuery("a[ng-action]", this.doc).live('click', function (event) {
this.docFindWithSelf("a[ng-action]").live('click', function (event) {
var jNode = jQuery(this);
try {
jNode.scope().eval(jNode.attr('ng-action'));
Expand Down
19 changes: 17 additions & 2 deletions src/Loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,6 @@ nglr.Loader.prototype.load = function() {
this.loadCss('/stylesheets/jquery-ui/smoothness/jquery-ui-1.7.1.css');
this.loadCss('/stylesheets/nglr.css');
console.log("Server: " + this.config.server);
jQuery.noConflict();
nglr.msie = jQuery.browser.msie;
this.configureJQueryPlugins();
this.computeConfiguration();
Expand Down Expand Up @@ -201,7 +200,7 @@ nglr.Loader.prototype.uid = function() {
nglr.Loader.prototype.computeConfiguration = function() {
var config = this.config;
if (!config.database) {
var match = config.server.match(/https?:\/\/([\w]*)/)
var match = config.server.match(/https?:\/\/([\w]*)/);
config.database = match ? match[1] : "$MEMORY";
}
};
Expand Down Expand Up @@ -387,3 +386,19 @@ nglr.UrlWatcher.prototype.setUrl = function(url) {
nglr.UrlWatcher.prototype.getUrl = function() {
return window.location.href;
};

window['angularFactory'] = function(config) {
var defaults = {
server: ""
};
//todo: don't load stylesheet by default
//todo: don't start watcher
function compile(root){
var loader = new nglr.Loader(root, jQuery("head"), _(defaults).extend(config));
loader.load();
return jQuery(root).scope();
};
return {
compile:compile
};
};
49 changes: 22 additions & 27 deletions src/angular-bootstrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,38 +39,32 @@
};

if (scriptConfig.autoLoadDependencies) {
addScript("/javascripts/webtoolkit.base64.js");
addScript("/javascripts/swfobject.js");
addScript("/javascripts/jQuery/jquery-1.3.2.js");
addScript("/javascripts/jQuery/jquery-ui-1.7.1.custom.min.js");
addScript("/javascripts/underscore/underscore.js");
addScript("/javascripts/nglr/Loader.js");
addScript("/javascripts/nglr/API.js");
addScript("/javascripts/nglr/Binder.js");
addScript("/javascripts/nglr/ControlBar.js");
addScript("/javascripts/nglr/DataStore.js");
addScript("/javascripts/nglr/Filters.js");
addScript("/javascripts/nglr/JSON.js");
addScript("/javascripts/nglr/Model.js");
addScript("/javascripts/nglr/Parser.js");
addScript("/javascripts/nglr/Scope.js");
addScript("/javascripts/nglr/Server.js");
addScript("/javascripts/nglr/Users.js");
addScript("/javascripts/nglr/Validators.js");
addScript("/javascripts/nglr/Widgets.js");
addScript("/../lib/webtoolkit/webtoolkit.base64.js");
addScript("/../lib/swfobject/swfobject.js");
addScript("/../lib/jquery/jquery-1.3.2.js");
addScript("/../lib/jquery/jquery-ui-1.7.1.custom.min.js");
addScript("/../lib/underscore/underscore.js");
addScript("/Loader.js");
addScript("/API.js");
addScript("/Binder.js");
addScript("/ControlBar.js");
addScript("/DataStore.js");
addScript("/Filters.js");
addScript("/JSON.js");
addScript("/Model.js");
addScript("/Parser.js");
addScript("/Scope.js");
addScript("/Server.js");
addScript("/Users.js");
addScript("/Validators.js");
addScript("/Widgets.js");
} else {
addScript("/ajax/libs/swfobject/2.2/swfobject.js", "http://ajax.googleapis.com");
addScript("/ajax/libs/jquery/1.3.2/jquery.min.js", "http://ajax.googleapis.com");
addScript("/ajax/libs/jqueryui/1.7.2/jquery-ui.min.js", "http://ajax.googleapis.com");
}

window.onload = function() {
window.angular.init = function(root, config){
var cnfgMerged = _.clone(scriptConfig||{});
_.extend(cnfgMerged, config);
new nglr.Loader(root, jQuery("head"), cnfgMerged).load();
};

var doc = window.document;
if (scriptConfig.bindRootId) {
doc = null;
Expand All @@ -86,12 +80,13 @@
doc = window.document.getElementById(id);
}
}
var angular = window.angularFactory(scriptConfig);
if (scriptConfig.autoBind && doc) {
window.angular.init(doc);
window.angularScope = angular.compile(doc);
}
if (typeof previousOnLoad === 'function') {
try {
previousOnLoad.apply(this, arguments);
previousOnLoad.apply(this, arguments);
} catch (e) {}
}
};
Expand Down
18 changes: 10 additions & 8 deletions src/test/Runner.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
if (!nglr.test) nglr.test = {};

nglr.test.ScenarioRunner = function(scenarios, body) {
this.scenarios = scenarios;
this.body = body;
Expand Down Expand Up @@ -46,10 +48,10 @@ nglr.test.Runner.prototype = {
scenario:jQuery('<div class="scenario"></div>')
};
current.run = current.scenario.append(
'<div class="run">' +
'<span class="name">.</span>' +
'<span class="time">.</span>' +
'<span class="state">.</span>' +
'<div class="run">' +
'<span class="name">.</span>' +
'<span class="time">.</span>' +
'<span class="state">.</span>' +
'</run>').find(".run");
current.log = current.scenario.append('<div class="log"></div>').find(".log");
current.run.find(".name").text(name);
Expand Down Expand Up @@ -79,7 +81,7 @@ nglr.test.Runner.prototype = {
log.text(buf.join(" "));
this.current.log.append(log);
this.console.scrollTop(this.console[0].scrollHeight);
if (level == "error")
if (level == "error")
this.current.error = buf.join(" ");
}
};
Expand Down Expand Up @@ -114,16 +116,16 @@ nglr.test.Scenario.prototype = {
else if (step.Then) fn = angular.test.THEN[step.Then];
return fn || function (){
throw "ERROR: Need Given/When/Then got: " + nglr.toJson(step);
};
};
},
context: function(runner) {
var frame = runner.frame;
var window = frame[0].contentWindow;
var document;
if (window.jQuery)
if (window.jQuery)
document = window.jQuery(window.document);
var context = {
frame:frame,
frame:frame,
window:window,
log:_.bind(runner.log, runner, "info"),
document:document,
Expand Down
1 change: 1 addition & 0 deletions test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
java -jar lib/jstestdriver/JsTestDriver.jar --tests all
12 changes: 12 additions & 0 deletions test/ExternalApiTest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
ExternalApiTest = TestCase("ExternalApiTest");

ExternalApiTest.prototype = {
testItShouldExposefactory:function(){
var node = $('<div ng-init="a=1">{{b=a+1}}</div>')[0];
var settings = {};
var angular = angularFactory(settings);
var scope = angular.compile(node);
assertEquals(1, scope.get('a'));
assertEquals(2, scope.get('b'));
}
};
4 changes: 0 additions & 4 deletions test/formsTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,3 @@ nglrTest.prototype.testBind = function(){
assertEquals(arg, 'arg');
}).apply('XXX', ['arg']);
};




8 changes: 7 additions & 1 deletion test/testabilityPatch.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ function noop(){}

jstd = jstestdriver;

swfobject = {
createSwf:function(){
fail("must mock out swfobject.createSwf in test.");
}
};

function html(content) {
return jQuery("<div></div>").html(content);
}
Expand Down Expand Up @@ -126,4 +132,4 @@ function assertThrows(error, fn){
fail("Expecting exception, none thrown");
}
assertEquals(error, exception);
}
}

0 comments on commit 214c142

Please sign in to comment.