work on $location and autobind

This commit is contained in:
Misko Hevery 2010-04-01 14:10:28 -07:00
parent 11a6431f89
commit 85f13d602e
8 changed files with 68 additions and 54 deletions

View file

@ -2,9 +2,9 @@
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<link rel="stylesheet" type="text/css" href="style.css"></link>
<script type="text/javascript" src="../src/angular-bootstrap.js#autobind&rootScope=$view"></script>
<script type="text/javascript" src="../src/angular-bootstrap.js#autobind"></script>
</head>
<body>
<body ng-init="$window.$scope = $root">
<table>
<tr>
<th>Description</th>

View file

@ -45,7 +45,7 @@ UrlWatcher.prototype = {
}
};
////////////////////////////////////
if (typeof document.getAttribute == 'undefined')
document.getAttribute = function() {};
@ -386,3 +386,20 @@ function compile(element, config) {
}
/////////////////////////////////////////////////
function parseKeyValue(keyValue) {
var obj = {}, key_value, key;
foreach((keyValue || "").split('&'), function(keyValue){
if (keyValue) {
key_value = keyValue.split('=');
key = decodeURIComponent(key_value[0]);
obj[key] = key_value[1] ? decodeURIComponent(key_value[1]) : true;
}
});
return obj;
}
function angularInit(config){
if (config.autobind) {
compile(window.document, config).$init();
}
}

View file

@ -22,23 +22,16 @@
* THE SOFTWARE.
*/
(function(previousOnLoad){
var filename = /(.*)\/angular-(.*).js(#(.*))?/;
var scripts = document.getElementsByTagName("SCRIPT");
var serverPath;
var config = {};
var filename = /(.*)\/angular-(.*).js(#(.*))?/,
scripts = document.getElementsByTagName("SCRIPT"),
serverPath,
config,
match;
for(var j = 0; j < scripts.length; j++) {
var match = (scripts[j].src || "").match(filename);
match = (scripts[j].src || "").match(filename);
if (match) {
serverPath = match[1];
parseConfig(match[4]);
}
}
function parseConfig(args) {
var keyValues = args.split('&'), keyValue, i = 0;
for (; i < keyValues.length; i++) {
keyValue = keyValues[i].split('=');
config[keyValue[0]] = keyValue[1] || true;
config = match[4];
}
}
@ -53,7 +46,6 @@
addScript("/jqlite.js");
addScript("/Parser.js");
addScript("/Resource.js");
addScript("/URLWatcher.js");
// Extension points
addScript("/apis.js");
@ -63,17 +55,14 @@
addScript("/directives.js");
addScript("/markups.js");
addScript("/widgets.js");
addScript("/services.js");
if (config.autobind) {
window.onload = function(){
try {
if (previousOnLoad) previousOnLoad();
} catch(e) {}
var scope = angular.compile(window.document, config);
if (config.rootScope) window[config.rootScope] = scope;
scope.$init();
};
}
window.onload = function(){
try {
if (previousOnLoad) previousOnLoad();
} catch(e) {}
angularInit(parseKeyValue(config));
};
})(window.onload);

View file

@ -51,6 +51,7 @@ angularTextMarkup('{{}}', function(text, textNode, parentElement) {
}
});
// TODO: this should be widget not a markup
angularTextMarkup('OPTION', function(text, textNode, parentElement){
if (parentElement[0].nodeName == "OPTION") {
var select = document.createElement('select');

View file

@ -1,34 +1,34 @@
angularService("$window", bind(window, identity, window));
angularService("$anchor", function(){
var URL_MATCH = /^(file|ftp|http|https):\/\/(\w+:{0,1}\w*@)?([\w\.]+)(:([0-9]+))?([^\?#]+)?(\?([^#]*))((#([^\?]*))(\?([^\?]*))?)$/;
angularService("$location", function(){
var scope = this;
function anchor(url){
function location(url){
if (isDefined(url)) {
if (url.charAt(0) == '#') url = url.substr(1);
var pathQuery = url.split('?');
anchor.path = decodeURIComponent(pathQuery[0]);
anchor.param = {};
foreach((pathQuery[1] || "").split('&'), function(keyValue){
if (keyValue) {
var parts = keyValue.split('=');
var key = decodeURIComponent(parts[0]);
var value = parts[1];
if (!value) value = true;
anchor.param[key] = decodeURIComponent(value);
}
});
var match = URL_MATCH.exec(url);
dump(match);
location.href = url;
location.protocol = match[1];
location.host = match[3];
location.port = match[5];
location.path = match[6];
location.search = parseKeyValue(match[8]);
location.hash = match[9];
location.hashPath = match[11];
location.hashSearch = parseKeyValue(match[13]);
foreach(location, dump);
}
var params = [];
foreach(anchor.param, function(value, key){
foreach(location.param, function(value, key){
params.push(encodeURIComponent(key) + '=' + encodeURIComponent(value));
});
return (anchor.path ? anchor.path : '') + (params.length ? '?' + params.join('&') : '');
return (location.path ? location.path : '') + (params.length ? '?' + params.join('&') : '');
};
this.$config.location.watch(function(url){
anchor(url);
location(url);
});
this.$onEval(PRIORITY_LAST, function(){
scope.$config.location.set(anchor());
scope.$config.location.set(location());
});
return anchor;
return location;
});

View file

@ -721,13 +721,13 @@ BinderTest.prototype.testItShouldSelectTheCorrectRadioBox = function() {
var male = jqLite(c.node[0].childNodes[1]);
female.click();
assertEquals("female", c.scope.$get("sex"));
assertEquals("female", c.scope.sex);
assertEquals(true, female[0].checked);
assertEquals(false, male[0].checked);
assertEquals("female", female.val());
male.click();
assertEquals("male", c.scope.$get("sex"));
assertEquals("male", c.scope.sex);
assertEquals(false, female[0].checked);
assertEquals(true, male[0].checked);
assertEquals("male", male.val());

View file

@ -13,10 +13,17 @@ describe("services", function(){
expect(scope.$window).toEqual(window);
});
it("should inject $anchor", function(){
scope.$anchor('#path?key=value');
expect(scope.$anchor.path).toEqual("path");
expect(scope.$anchor.param).toEqual({key:'value'});
it("should inject $location", function(){
scope.$location('http://host:1234/p/a/t/h?query=value#path?key=value');
expect(scope.$location.href).toEqual("http://host:123/p/a/t/h?query=value#path?key=value");
expect(scope.$location.protocol).toEqual("http");
expect(scope.$location.host).toEqual("host");
expect(scope.$location.port).toEqual("1234");
expect(scope.$location.path).toEqual("/p/a/t/h");
expect(scope.$location.search).toEqual({query:'value'});
expect(scope.$location.hash).toEqual('path?key=value');
expect(scope.$location.hashPath).toEqual('path');
expect(scope.$location.hashSearch).toEqual({key:'value'});
scope.$anchor.path = 'page=http://path';
scope.$anchor.param = {k:'a=b'};