Skip to content
Browse files

Set current state

  • Loading branch information...
1 parent 8b8445f commit d0bebacdb5f5d49143d078c82a1447e0419ab569 @vic committed Aug 9, 2012
Showing with 10 additions and 8 deletions.
  1. +4 −3 index.coffee
  2. +5 −4 knockout-routes.js
  3. +1 −1 knockout-routes.min.js
View
7 index.coffee
@@ -12,7 +12,7 @@ ko.routes = (mapping)->
Routes = ->
Routes.prototype = {}
- current_url = ko.observable()
+ current = ko.observable()
routes = new Routes
for own name, route of mapping when typeof route == 'string'
@@ -37,7 +37,7 @@ ko.routes = (mapping)->
u = u.replace(suid, '')
matches = u.match(regexp)
if matches
- current_url url()
+ current state
unless data
data = {}
data[names[i]] = value for value, i in matches.slice(1)
@@ -61,11 +61,12 @@ ko.routes = (mapping)->
state.url = url
state.param = params
- state.active = ko.computed read: -> url() == current_url()
+ state.active = ko.computed read: -> url() == current().url()
routes[name] = state
)(name, route)
+ Routes.prototype.current = current
Routes.prototype.ready = (_sigil)->
sigil _sigil if _sigil
state = History.getState()
View
9 knockout-routes.js
@@ -2,7 +2,7 @@
var __hasProp = {}.hasOwnProperty;
ko.routes = function(mapping) {
- var Routes, current_url, name, named, namedGroup, namedOrSplat, namedOrSplatGroup, route, routes, sigil, splat, splatGroup, suid;
+ var Routes, current, name, named, namedGroup, namedOrSplat, namedOrSplatGroup, route, routes, sigil, splat, splatGroup, suid;
named = /:\w+/g;
splat = /\*\w+/g;
namedOrSplat = /[:\*]\w+/;
@@ -13,7 +13,7 @@
sigil = ko.observable('');
Routes = function() {};
Routes.prototype = {};
- current_url = ko.observable();
+ current = ko.observable();
routes = new Routes;
for (name in mapping) {
if (!__hasProp.call(mapping, name)) continue;
@@ -70,7 +70,7 @@
u = u.replace(suid, '');
matches = u.match(regexp);
if (matches) {
- current_url(url());
+ current(state);
if (!data) {
data = {};
_ref1 = matches.slice(1);
@@ -142,13 +142,14 @@
state.param = params;
state.active = ko.computed({
read: function() {
- return url() === current_url();
+ return url() === current().url();
}
});
return routes[name] = state;
})(name, route);
}
}
+ Routes.prototype.current = current;
Routes.prototype.ready = function(_sigil) {
var n, r, state, url, _results;
if (_sigil) {
View
2 knockout-routes.min.js
@@ -1 +1 @@
-(function(){var e={}.hasOwnProperty;ko.routes=function(t){var n,r,i,s,o,u,a,f,l,c,h,p,d;s=/:\w+/g,h=/\*\w+/g,u=/[:\*]\w+/,a=/(:|\*)(\w+)/g,o="([^/]+)",p="(.*?)",d=/[\?]&_suid=\d+/,c=ko.observable(""),n=function(){},n.prototype={},r=ko.observable(),l=new n;for(i in t){if(!e.call(t,i))continue;f=t[i],typeof f=="string"&&function(t,n){var i,f,v,m,g,y,b,w,E,S,x,T,N,C,k,L,A;g={},m=function(){var e,t,r,i;r=n.match(a)||[],i=[];for(e=0,t=r.length;e<t;e++)E=r[e],i.push(E.slice(1));return i}();for(N=0,k=m.length;N<k;N++)v=m[N],g[v]=ko.observable();b=[],A=n.split(u),T=function(e,t){b.push(function(){return e});if(m[t])return b.push(g[m[t]])};for(f=C=0,L=A.length;C<L;f=++C)y=A[f],T(y,f);return i=n.replace(s,o).replace(h,p),w=new RegExp("^[#?/]*?"+i+"$"),x=ko.computed({write:function(t,n,i,s,o){var u,a,l,c,h,p,v;n==null&&(n=null),i==null&&(i=!0),s==null&&(s=!1),o==null&&(o=null),t=t.replace(d,""),a=t.match(w);if(a){r(x());if(!n){n={},v=a.slice(1);for(f=h=0,p=v.length;h<p;f=++h)c=v[f],n[m[f]]=c}for(u in n){if(!e.call(n,u))continue;l=n[u],g[u]&&g[u](l)}return S(n,i,s,o)}},read:function(){var e;return c()+function(){var t,n,r;r=[];for(t=0,n=b.length;t<n;t++)e=b[t],r.push(e());return r}().join("")}}),S=function(){var t;return t=ko.observable(!1),ko.computed({write:function(n,r,i,s){var o,u;r==null&&(r=!0),i==null&&(i=!1),s==null&&(s=null),t(n);for(o in n){if(!e.call(n,o))continue;u=n[o],g[o]&&g[o](u)}if(r)return i?History.replaceState(n,s,x()):History.pushState(n,s,x())},read:function(){return t()}})}(),S.url=x,S.param=g,S.active=ko.computed({read:function(){return x()===r()}}),l[t]=S}(i,f)}return n.prototype.ready=function(t){var n,r,i,s,o;t&&c(t),i=History.getState(),s=i.hash,o=[];for(n in l){if(!e.call(l,n))continue;r=l[n],o.push(r.url(s,!1,!1))}return o},History.Adapter.bind(window,"statechange",function(){var t,n,r,i,s;r=History.getState(),i=r.hash,s=[];for(t in l){if(!e.call(l,t))continue;n=l[t],s.push(n.url(i,r.data))}return s}),l}}).call(this)
+(function(){var e={}.hasOwnProperty;ko.routes=function(t){var n,r,i,s,o,u,a,f,l,c,h,p,d;s=/:\w+/g,h=/\*\w+/g,u=/[:\*]\w+/,a=/(:|\*)(\w+)/g,o="([^/]+)",p="(.*?)",d=/[\?]&_suid=\d+/,c=ko.observable(""),n=function(){},n.prototype={},r=ko.observable(),l=new n;for(i in t){if(!e.call(t,i))continue;f=t[i],typeof f=="string"&&function(t,n){var i,f,v,m,g,y,b,w,E,S,x,T,N,C,k,L,A;g={},m=function(){var e,t,r,i;r=n.match(a)||[],i=[];for(e=0,t=r.length;e<t;e++)E=r[e],i.push(E.slice(1));return i}();for(N=0,k=m.length;N<k;N++)v=m[N],g[v]=ko.observable();b=[],A=n.split(u),T=function(e,t){b.push(function(){return e});if(m[t])return b.push(g[m[t]])};for(f=C=0,L=A.length;C<L;f=++C)y=A[f],T(y,f);return i=n.replace(s,o).replace(h,p),w=new RegExp("^[#?/]*?"+i+"$"),x=ko.computed({write:function(t,n,i,s,o){var u,a,l,c,h,p,v;n==null&&(n=null),i==null&&(i=!0),s==null&&(s=!1),o==null&&(o=null),t=t.replace(d,""),a=t.match(w);if(a){r(S);if(!n){n={},v=a.slice(1);for(f=h=0,p=v.length;h<p;f=++h)c=v[f],n[m[f]]=c}for(u in n){if(!e.call(n,u))continue;l=n[u],g[u]&&g[u](l)}return S(n,i,s,o)}},read:function(){var e;return c()+function(){var t,n,r;r=[];for(t=0,n=b.length;t<n;t++)e=b[t],r.push(e());return r}().join("")}}),S=function(){var t;return t=ko.observable(!1),ko.computed({write:function(n,r,i,s){var o,u;r==null&&(r=!0),i==null&&(i=!1),s==null&&(s=null),t(n);for(o in n){if(!e.call(n,o))continue;u=n[o],g[o]&&g[o](u)}if(r)return i?History.replaceState(n,s,x()):History.pushState(n,s,x())},read:function(){return t()}})}(),S.url=x,S.param=g,S.active=ko.computed({read:function(){return x()===r().url()}}),l[t]=S}(i,f)}return n.prototype.current=r,n.prototype.ready=function(t){var n,r,i,s,o;t&&c(t),i=History.getState(),s=i.hash,o=[];for(n in l){if(!e.call(l,n))continue;r=l[n],o.push(r.url(s,!1,!1))}return o},History.Adapter.bind(window,"statechange",function(){var t,n,r,i,s;r=History.getState(),i=r.hash,s=[];for(t in l){if(!e.call(l,t))continue;n=l[t],s.push(n.url(i,r.data))}return s}),l}}).call(this)

0 comments on commit d0bebac

Please sign in to comment.
Something went wrong with that request. Please try again.