jim 0.2.3 → 0.3.0.pre
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +2 -1
- data/Gemfile.lock +2 -0
- data/HISTORY +13 -0
- data/README.md +148 -0
- data/Rakefile +6 -3
- data/bin/jim +1 -2
- data/default +0 -0
- data/jim.gemspec +142 -105
- data/lib/jim.rb +1 -1
- data/lib/jim/bundler.rb +168 -73
- data/lib/jim/cli.rb +200 -151
- data/lib/jim/index.rb +20 -9
- data/lib/jim/installer.rb +46 -46
- data/lib/jim/rack.rb +57 -20
- data/lib/jim/templates/jimfile +11 -5
- data/lib/jim/version_parser.rb +3 -3
- data/test/fixtures/infoincomments.js +1 -1
- data/test/fixtures/jimfile +14 -7
- data/test/fixtures/jquery-1.4.1.js +3 -6057
- data/test/fixtures/jquery.color.js +1 -1
- data/test/fixtures/localfile.js +1 -1
- data/test/fixtures/mustache.js/package.json +1 -1
- data/test/fixtures/noversion.js +1 -1
- data/test/fixtures/old_jimfile +7 -0
- data/test/fixtures/sammy-0.5.0/examples/backend/app.rb +4 -4
- data/test/fixtures/sammy-0.5.0/examples/backend/public/javascripts/app.js +18 -18
- data/test/fixtures/sammy-0.5.0/examples/backend/public/javascripts/jquery.cloudkit.js +1 -1
- data/test/fixtures/sammy-0.5.0/examples/backend/public/javascripts/jquery.js +1 -1
- data/test/fixtures/sammy-0.5.0/examples/backend/public/javascripts/sammy.js +161 -161
- data/test/fixtures/sammy-0.5.0/examples/backend/public/templates/task.html.erb +1 -1
- data/test/fixtures/sammy-0.5.0/examples/backend/public/templates/task_details.html.erb +1 -1
- data/test/fixtures/sammy-0.5.0/examples/backend/views/app.sass +6 -6
- data/test/fixtures/sammy-0.5.0/examples/backend/views/index.haml +5 -5
- data/test/fixtures/sammy-0.5.0/examples/form_handling/index.html +16 -16
- data/test/fixtures/sammy-0.5.0/examples/hello_world/index.html +13 -13
- data/test/fixtures/sammy-0.5.0/examples/location_override/data.html +28 -28
- data/test/fixtures/sammy-0.5.0/examples/location_override/index.html +18 -18
- data/test/fixtures/sammy-0.5.0/examples/location_override/test.html +36 -36
- data/test/fixtures/sammy-0.5.0/lib/min/sammy-0.5.0.min.js +1 -1
- data/test/fixtures/sammy-0.5.0/lib/min/sammy-lastest.min.js +1 -1
- data/test/fixtures/sammy-0.5.0/lib/plugins/sammy.cache.js +13 -13
- data/test/fixtures/sammy-0.5.0/lib/plugins/sammy.haml.js +2 -2
- data/test/fixtures/sammy-0.5.0/lib/plugins/sammy.json.js +15 -15
- data/test/fixtures/sammy-0.5.0/lib/plugins/sammy.mustache.js +46 -46
- data/test/fixtures/sammy-0.5.0/lib/plugins/sammy.nested_params.js +29 -29
- data/test/fixtures/sammy-0.5.0/lib/plugins/sammy.storage.js +54 -54
- data/test/fixtures/sammy-0.5.0/lib/plugins/sammy.template.js +17 -17
- data/test/fixtures/sammy-0.5.0/lib/sammy.js +220 -220
- data/test/fixtures/sammy-0.5.0/test/fixtures/partial.html +1 -1
- data/test/fixtures/sammy-0.5.0/test/index.html +26 -26
- data/test/fixtures/sammy-0.5.0/test/test_sammy_application.js +60 -60
- data/test/fixtures/sammy-0.5.0/test/test_sammy_event_context.js +21 -21
- data/test/fixtures/sammy-0.5.0/test/test_sammy_location_proxy.js +3 -3
- data/test/fixtures/sammy-0.5.0/test/test_sammy_plugins.js +17 -17
- data/test/fixtures/sammy-0.5.0/test/test_sammy_storage.js +4 -4
- data/test/helper.rb +15 -0
- data/test/test_jim_bundler.rb +114 -74
- data/test/test_jim_cli.rb +34 -18
- data/test/test_jim_index.rb +19 -19
- data/test/test_jim_installer.rb +13 -13
- data/test/test_jim_rack.rb +41 -0
- data/test/test_jim_version_parser.rb +4 -4
- metadata +204 -41
- data/.gitignore +0 -24
- data/README.rdoc +0 -105
- data/lib/jim/templates/commands +0 -58
@@ -2,4 +2,4 @@
|
|
2
2
|
// http://code.quirkey.com/sammy
|
3
3
|
// Version: 0.5.0
|
4
4
|
// Built: Mon Feb 15 14:13:25 -0500 2010
|
5
|
-
(function(g){var d="([^/]+)",f=/:([\w\d]+)/g,c=/\?([^#]*)$/,a=decodeURIComponent,b=function(h){return function(i,j){return this.route.apply(this,[h,i,j])}},e=[];Sammy=function(){var i=g.makeArray(arguments),j,h;Sammy.apps=Sammy.apps||{};if(i.length==0||i[0]&&g.isFunction(i[0])){return Sammy.apply(Sammy,["body"].concat(i))}else{if(typeof(h=i.shift())=="string"){j=Sammy.apps[h]||new Sammy.Application();j.element_selector=h;if(i.length>0){g.each(i,function(k,l){j.use(l)})}if(j.element_selector!=h){delete Sammy.apps[h]}Sammy.apps[j.element_selector]=j;return j}}};Sammy.VERSION="0.5.0";Sammy.addLogger=function(h){e.push(h)};Sammy.log=function(){var h=g.makeArray(arguments);h.unshift("["+Date()+"]");g.each(e,function(k,j){j.apply(Sammy,h)})};if(typeof window.console!="undefined"){if(g.isFunction(console.log.apply)){Sammy.addLogger(function(){window.console.log.apply(console,arguments)})}else{Sammy.addLogger(function(){window.console.log(arguments)})}}else{if(typeof console!="undefined"){Sammy.addLogger(function(){console.log.apply(console,arguments)})}}Sammy.Object=function(h){return g.extend(this,h||{})};g.extend(Sammy.Object.prototype,{toHash:function(){var h={};g.each(this,function(j,i){if(!g.isFunction(i)){h[j]=i}});return h},toHTML:function(){var h="";g.each(this,function(j,i){if(!g.isFunction(i)){h+="<strong>"+j+"</strong> "+i+"<br />"}});return h},uuid:function(){if(typeof this._uuid=="undefined"||!this._uuid){this._uuid=(new Date()).getTime()+"-"+parseInt(Math.random()*1000)}return this._uuid},keys:function(h){var i=[];for(var j in this){if(!g.isFunction(this[j])||!h){i.push(j)}}return i},has:function(h){return this[h]&&g.trim(this[h].toString())!=""},join:function(){var i=g.makeArray(arguments);var h=i.shift();return i.join(h)},log:function(){Sammy.log.apply(Sammy,arguments)},toString:function(h){var i=[];g.each(this,function(l,j){if(!g.isFunction(j)||h){i.push('"'+l+'": '+j.toString())}});return"Sammy.Object: {"+i.join(",")+"}"}});Sammy.HashLocationProxy=function(i,h){this.app=i;if("onhashchange" in window){Sammy.log("native hash change exists, using");this.is_native=true}else{Sammy.log("no native hash change, falling back to polling");this.is_native=false;this._startPolling(h)}};Sammy.HashLocationProxy.prototype={bind:function(){var h=this.app;g(window).bind("hashchange."+this.app.eventNamespace(),function(){h.trigger("location-changed")})},unbind:function(){g(window).die("hashchange."+this.app.eventNamespace())},getLocation:function(){var h=window.location.toString().match(/^[^#]*(#.+)$/);return h?h[1]:""},setLocation:function(h){return window.location=h},_startPolling:function(j){var i=this;if(!Sammy.HashLocationProxy._interval){if(!j){j=10}var h=function(){current_location=i.getLocation();if(!Sammy.HashLocationProxy._last_location||current_location!=Sammy.HashLocationProxy._last_location){setTimeout(function(){g(window).trigger("hashchange")},1)}Sammy.HashLocationProxy._last_location=current_location};h();Sammy.HashLocationProxy._interval=setInterval(h,j);g(window).bind("unload",function(){clearInterval(Sammy.HashLocationProxy._interval)})}}};Sammy.DataLocationProxy=function(i,h){this.app=i;this.data_name=h||"sammy-location"};Sammy.DataLocationProxy.prototype={bind:function(){var h=this;this.app.$element().bind("setData",function(j,i){if(i==h.data_name){h.app.trigger("location-changed")}})},unbind:function(){this.app.$element().die("setData")},getLocation:function(){return this.app.$element().data(this.data_name)},setLocation:function(h){return this.app.$element().data(this.data_name,h)}};Sammy.Application=function(h){var i=this;this.routes={};this.listeners=new Sammy.Object({});this.arounds=[];this.befores=[];this.namespace=this.uuid();this.context_prototype=function(){Sammy.EventContext.apply(this,arguments)};this.context_prototype.prototype=new Sammy.EventContext();if(g.isFunction(h)){h.apply(this,[this])}if(!this.location_proxy){this.location_proxy=new Sammy.HashLocationProxy(i,this.run_interval_every)}if(this.debug){this.bindToAllEvents(function(k,j){i.log(i.toString(),k.cleaned_type,j||{})})}};Sammy.Application.prototype=g.extend({},Sammy.Object.prototype,{ROUTE_VERBS:["get","post","put","delete"],APP_EVENTS:["run","unload","lookup-route","run-route","route-found","event-context-before","event-context-after","changed","error-404","check-form-submission","redirect"],_last_route:null,_running:false,data_store_name:"sammy-app",element_selector:"body",debug:false,silence_404:true,run_interval_every:50,location_proxy:null,template_engine:null,toString:function(){return"Sammy.Application:"+this.element_selector},$element:function(){return g(this.element_selector)},use:function(){var h=g.makeArray(arguments);var i=h.shift();try{h.unshift(this);i.apply(this,h)}catch(j){if(typeof i=="undefined"){throw ("Error: called use() but plugin is not defined")}else{if(!g.isFunction(i)){throw ("Error: called use() but '"+i.toString()+"' is not a function")}else{throw (j)}}}return this},route:function(k,i,m){var j=this,l=[],h;if(!m&&g.isFunction(i)){i=k;m=i;k="any"}k=k.toLowerCase();if(i.constructor==String){f.lastIndex=0;while((path_match=f.exec(i))!=null){l.push(path_match[1])}i=new RegExp("^"+i.replace(f,d)+"$")}if(typeof m=="string"){m=j[m]}h=function(n){var o={verb:n,path:i,callback:m,param_names:l};j.routes[n]=j.routes[n]||[];j.routes[n].push(o)};if(k==="any"){g.each(this.ROUTE_VERBS,function(o,n){h(n)})}else{h(k)}return this},get:b("get"),post:b("post"),put:b("put"),del:b("delete"),any:b("any"),mapRoutes:function(i){var h=this;g.each(i,function(j,k){h.route.apply(h,k)});return this},eventNamespace:function(){return[this.data_store_name,this.namespace].join("-")},bind:function(h,j,l){var k=this;if(typeof l=="undefined"){l=j}var i=function(){var o,m,n;o=arguments[0];n=arguments[1];if(n&&n.context){m=n.context;delete n.context}else{m=new k.context_prototype(k,"bind",o.type,n)}o.cleaned_type=o.type.replace(k.eventNamespace(),"");l.apply(m,[o,n])};if(!this.listeners[h]){this.listeners[h]=[]}this.listeners[h].push(i);if(this.isRunning()){this._listen(h,i)}return this},trigger:function(h,i){this.$element().trigger([h,this.eventNamespace()].join("."),[i]);return this},refresh:function(){this.last_location=null;this.trigger("location-changed");return this},before:function(h,i){if(g.isFunction(h)){i=h;h={}}this.befores.push([h,i]);return this},after:function(h){return this.bind("event-context-after",h)},around:function(h){this.arounds.push(h);return this},isRunning:function(){return this._running},helpers:function(h){g.extend(this.context_prototype.prototype,h);return this},helper:function(h,i){this.context_prototype.prototype[h]=i;return this},run:function(h){if(this.isRunning()){return false}var i=this;g.each(this.listeners.toHash(),function(j,k){g.each(k,function(m,l){i._listen(j,l)})});this.trigger("run",{start_url:h});this._running=true;this.$element().data(this.data_store_name,this);this.last_location=null;if(this.getLocation()==""&&typeof h!="undefined"){this.setLocation(h)}this._checkLocation();this.location_proxy.bind();this.bind("location-changed",function(){i._checkLocation()});this.bind("submit",function(k){var j=i._checkFormSubmission(g(k.target).closest("form"));return(j===false)?k.preventDefault():false});g("body").bind("onunload",function(){i.unload()});return this.trigger("changed")},unload:function(){if(!this.isRunning()){return false}var h=this;this.trigger("unload");this.location_proxy.unbind();this.$element().unbind("submit").removeClass(h.eventNamespace());this.$element().removeData(this.data_store_name);g.each(this.listeners.toHash(),function(i,j){g.each(j,function(l,k){h._unlisten(i,k)})});this._running=false;return this},bindToAllEvents:function(i){var h=this;g.each(this.APP_EVENTS,function(j,k){h.bind(k,i)});g.each(this.listeners.keys(true),function(k,j){if(h.APP_EVENTS.indexOf(j)==-1){h.bind(j,i)}});return this},routablePath:function(h){return h.replace(c,"")},lookupRoute:function(k,i){var j=this,h=false;this.trigger("lookup-route",{verb:k,path:i});if(typeof this.routes[k]!="undefined"){g.each(this.routes[k],function(m,l){if(j.routablePath(i).match(l.path)){h=l;return false}})}return h},runRoute:function(i,r,k){this.log("runRoute",[i,r].join(" "));this.trigger("run-route",{verb:i,path:r,params:k});if(typeof k=="undefined"){k={}}g.extend(k,this._parseQueryString(r));var j=this,h,n,l,m,q,o,p=this.lookupRoute(i,r);if(p){this.trigger("route-found",{route:p});if((path_params=p.path.exec(this.routablePath(r)))!=null){path_params.shift();g.each(path_params,function(s,t){if(p.param_names[s]){k[p.param_names[s]]=a(t)}else{if(!k.splat){k.splat=[]}k.splat.push(a(t))}})}h=new this.context_prototype(this,i,r,k);l=this.arounds.slice(0);q=this.befores.slice(0);n=function(){var s;while(q.length>0){o=q.shift();if(j.contextMatchesOptions(h,o[0])){s=o[1].apply(h,[h]);if(s===false){return false}}}j.last_route=p;h.trigger("event-context-before",{context:h});s=p.callback.apply(h,[h]);h.trigger("event-context-after",{context:h});return s};g.each(l.reverse(),function(s,t){var u=n;n=function(){return t.apply(h,[u])}});return n()}else{this.notFound(i,r)}},contextMatchesOptions:function(k,m,i){var j=m;if(typeof j==="undefined"||j=={}){return true}if(typeof i==="undefined"){i=true}if(typeof j==="string"||g.isFunction(j.test)){j={path:j}}if(j.only){return this.contextMatchesOptions(k,j.only,true)}else{if(j.except){return this.contextMatchesOptions(k,j.except,false)}}var h=true,l=true;if(j.path){if(g.isFunction(j.path.test)){h=j.path.test(k.path)}else{h=(j.path.toString()===k.path)}}if(j.verb){l=j.verb===k.verb}return i?(l&&h):!(l&&h)},getLocation:function(){return this.location_proxy.getLocation()},setLocation:function(h){return this.location_proxy.setLocation(h)},swap:function(h){return this.$element().html(h)},notFound:function(i,h){this.trigger("error-404",{verb:i,path:h});throw ("404 Not Found "+i+" "+h)},_checkLocation:function(){try{var h,i;h=this.getLocation();if(h!=this.last_location){i=this.runRoute("get",h)}this.last_location=h}catch(j){this.last_location=h;if(j.toString().match(/^404/)&&this.silence_404){return i}else{throw (j)}}return i},_checkFormSubmission:function(j){var h,l,n,m,i;this.trigger("check-form-submission",{form:j});h=g(j);l=h.attr("action");n=g.trim(h.attr("method").toString().toLowerCase());if(!n||n==""){n="get"}this.log("_checkFormSubmission",h,l,n);m=g.extend({},this._parseFormParams(h),{"$form":h});try{i=this.runRoute(n,l,m)}catch(k){if(k.toString().match(/^404/)&&this.silence_404){return true}else{throw (k)}}return(typeof i=="undefined")?false:i},_parseFormParams:function(h){var i={};g.each(h.serializeArray(),function(j,k){if(i[k.name]){if(g.isArray(i[k.name])){i[k.name].push(k.value)}else{i[k.name]=[i[k.name],k.value]}}else{i[k.name]=k.value}});return i},_parseQueryString:function(m){var k={},l,j,n,h;l=m.match(c);if(l){j=l[1].split("&");for(h=0;h<j.length;h+=1){n=j[h].split("=");k[n[0]]=a(n[1])}}return k},_listen:function(h,i){return this.$element().bind([h,this.eventNamespace()].join("."),i)},_unlisten:function(h,i){return this.$element().unbind([h,this.eventNamespace()].join("."),i)}});Sammy.EventContext=function(k,j,h,i){this.app=k;this.verb=j;this.path=h;this.params=new Sammy.Object(i)};Sammy.EventContext.prototype=g.extend({},Sammy.Object.prototype,{$element:function(){return this.app.$element()},partial:function(p,l,o){var h,k,n,m,j="partial:"+p,i=this;if((n=p.match(/\.([^\.]+)$/))){n=n[1]}if((!n||!g.isFunction(i[n]))&&this.app.template_engine){n=this.app.template_engine}if(n&&!g.isFunction(n)&&g.isFunction(i[n])){n=i[n]}if(!o&&g.isFunction(l)){o=l;l={}}m=(g.isArray(l)?l:[l||{}]),k=function(q){var r=q,s="";g.each(m,function(t,u){g.extend(u,i);if(g.isFunction(n)){r=n.apply(i,[q,u])}s+=r;if(o){return o.apply(i,[r,t])}});if(!o){i.app.swap(s)}i.trigger("changed")};if(this.app.cache_partials&&this.cache(j)){k.apply(i,[this.cache(j)])}else{g.get(p,function(q){if(i.app.cache_partials){i.cache(j,q)}k.apply(i,[q])})}},redirect:function(){var j,i=g.makeArray(arguments),h=this.app.getLocation();if(i.length>1){i.unshift("/");j=this.join.apply(this,i)}else{j=i[0]}this.trigger("redirect",{to:j});this.app.last_location=this.path;this.app.setLocation(j);if(h==j){this.app.trigger("location-changed")}},trigger:function(h,i){if(typeof i=="undefined"){i={}}if(!i.context){i.context=this}return this.app.trigger(h,i)},eventNamespace:function(){return this.app.eventNamespace()},notFound:function(){return this.app.notFound(this.verb,this.path)},toString:function(){return"Sammy.EventContext: "+[this.verb,this.path,this.params].join(" ")}});g.sammy=Sammy})(jQuery);
|
5
|
+
(function(g){var d="([^/]+)",f=/:([\w\d]+)/g,c=/\?([^#]*)$/,a=decodeURIComponent,b=function(h){return function(i,j){return this.route.apply(this,[h,i,j])}},e=[];Sammy=function(){var i=g.makeArray(arguments),j,h;Sammy.apps=Sammy.apps||{};if(i.length==0||i[0]&&g.isFunction(i[0])){return Sammy.apply(Sammy,["body"].concat(i))}else{if(typeof(h=i.shift())=="string"){j=Sammy.apps[h]||new Sammy.Application();j.element_selector=h;if(i.length>0){g.each(i,function(k,l){j.use(l)})}if(j.element_selector!=h){delete Sammy.apps[h]}Sammy.apps[j.element_selector]=j;return j}}};Sammy.VERSION="0.5.0";Sammy.addLogger=function(h){e.push(h)};Sammy.log=function(){var h=g.makeArray(arguments);h.unshift("["+Date()+"]");g.each(e,function(k,j){j.apply(Sammy,h)})};if(typeof window.console!="undefined"){if(g.isFunction(console.log.apply)){Sammy.addLogger(function(){window.console.log.apply(console,arguments)})}else{Sammy.addLogger(function(){window.console.log(arguments)})}}else{if(typeof console!="undefined"){Sammy.addLogger(function(){console.log.apply(console,arguments)})}}Sammy.Object=function(h){return g.extend(this,h||{})};g.extend(Sammy.Object.prototype,{toHash:function(){var h={};g.each(this,function(j,i){if(!g.isFunction(i)){h[j]=i}});return h},toHTML:function(){var h="";g.each(this,function(j,i){if(!g.isFunction(i)){h+="<strong>"+j+"</strong> "+i+"<br />"}});return h},uuid:function(){if(typeof this._uuid=="undefined"||!this._uuid){this._uuid=(new Date()).getTime()+"-"+parseInt(Math.random()*1000)}return this._uuid},keys:function(h){var i=[];for(var j in this){if(!g.isFunction(this[j])||!h){i.push(j)}}return i},has:function(h){return this[h]&&g.trim(this[h].toString())!=""},join:function(){var i=g.makeArray(arguments);var h=i.shift();return i.join(h)},log:function(){Sammy.log.apply(Sammy,arguments)},toString:function(h){var i=[];g.each(this,function(l,j){if(!g.isFunction(j)||h){i.push('"'+l+'": '+j.toString())}});return"Sammy.Object: {"+i.join(",")+"}"}});Sammy.HashLocationProxy=function(i,h){this.app=i;if("onhashchange" in window){Sammy.log("native hash change exists, using");this.is_native=true}else{Sammy.log("no native hash change, falling back to polling");this.is_native=false;this._startPolling(h)}};Sammy.HashLocationProxy.prototype={bind:function(){var h=this.app;g(window).bind("hashchange."+this.app.eventNamespace(),function(){h.trigger("location-changed")})},unbind:function(){g(window).die("hashchange."+this.app.eventNamespace())},getLocation:function(){var h=window.location.toString().match(/^[^#]*(#.+)$/);return h?h[1]:""},setLocation:function(h){return window.location=h},_startPolling:function(j){var i=this;if(!Sammy.HashLocationProxy._interval){if(!j){j=10}var h=function(){current_location=i.getLocation();if(!Sammy.HashLocationProxy._last_location||current_location!=Sammy.HashLocationProxy._last_location){setTimeout(function(){g(window).trigger("hashchange")},1)}Sammy.HashLocationProxy._last_location=current_location};h();Sammy.HashLocationProxy._interval=setInterval(h,j);g(window).bind("unload",function(){clearInterval(Sammy.HashLocationProxy._interval)})}}};Sammy.DataLocationProxy=function(i,h){this.app=i;this.data_name=h||"sammy-location"};Sammy.DataLocationProxy.prototype={bind:function(){var h=this;this.app.$element().bind("setData",function(j,i){if(i==h.data_name){h.app.trigger("location-changed")}})},unbind:function(){this.app.$element().die("setData")},getLocation:function(){return this.app.$element().data(this.data_name)},setLocation:function(h){return this.app.$element().data(this.data_name,h)}};Sammy.Application=function(h){var i=this;this.routes={};this.listeners=new Sammy.Object({});this.arounds=[];this.befores=[];this.namespace=this.uuid();this.context_prototype=function(){Sammy.EventContext.apply(this,arguments)};this.context_prototype.prototype=new Sammy.EventContext();if(g.isFunction(h)){h.apply(this,[this])}if(!this.location_proxy){this.location_proxy=new Sammy.HashLocationProxy(i,this.run_interval_every)}if(this.debug){this.bindToAllEvents(function(k,j){i.log(i.toString(),k.cleaned_type,j||{})})}};Sammy.Application.prototype=g.extend({},Sammy.Object.prototype,{ROUTE_VERBS:["get","post","put","delete"],APP_EVENTS:["run","unload","lookup-route","run-route","route-found","event-context-before","event-context-after","changed","error-404","check-form-submission","redirect"],_last_route:null,_running:false,data_store_name:"sammy-app",element_selector:"body",debug:false,silence_404:true,run_interval_every:50,location_proxy:null,template_engine:null,toString:function(){return"Sammy.Application:"+this.element_selector},$element:function(){return g(this.element_selector)},use:function(){var h=g.makeArray(arguments);var i=h.shift();try{h.unshift(this);i.apply(this,h)}catch(j){if(typeof i=="undefined"){throw ("Error: called use() but plugin is not defined")}else{if(!g.isFunction(i)){throw ("Error: called use() but '"+i.toString()+"' is not a function")}else{throw (j)}}}return this},route:function(k,i,m){var j=this,l=[],h;if(!m&&g.isFunction(i)){i=k;m=i;k="any"}k=k.toLowerCase();if(i.constructor==String){f.lastIndex=0;while((path_match=f.exec(i))!=null){l.push(path_match[1])}i=new RegExp("^"+i.replace(f,d)+"$")}if(typeof m=="string"){m=j[m]}h=function(n){var o={verb:n,path:i,callback:m,param_names:l};j.routes[n]=j.routes[n]||[];j.routes[n].push(o)};if(k==="any"){g.each(this.ROUTE_VERBS,function(o,n){h(n)})}else{h(k)}return this},get:b("get"),post:b("post"),put:b("put"),del:b("delete"),any:b("any"),mapRoutes:function(i){var h=this;g.each(i,function(j,k){h.route.apply(h,k)});return this},eventNamespace:function(){return[this.data_store_name,this.namespace].join("-")},bind:function(h,j,l){var k=this;if(typeof l=="undefined"){l=j}var i=function(){var o,m,n;o=arguments[0];n=arguments[1];if(n&&n.context){m=n.context;delete n.context}else{m=new k.context_prototype(k,"bind",o.type,n)}o.cleaned_type=o.type.replace(k.eventNamespace(),"");l.apply(m,[o,n])};if(!this.listeners[h]){this.listeners[h]=[]}this.listeners[h].push(i);if(this.isRunning()){this._listen(h,i)}return this},trigger:function(h,i){this.$element().trigger([h,this.eventNamespace()].join("."),[i]);return this},refresh:function(){this.last_location=null;this.trigger("location-changed");return this},before:function(h,i){if(g.isFunction(h)){i=h;h={}}this.befores.push([h,i]);return this},after:function(h){return this.bind("event-context-after",h)},around:function(h){this.arounds.push(h);return this},isRunning:function(){return this._running},helpers:function(h){g.extend(this.context_prototype.prototype,h);return this},helper:function(h,i){this.context_prototype.prototype[h]=i;return this},run:function(h){if(this.isRunning()){return false}var i=this;g.each(this.listeners.toHash(),function(j,k){g.each(k,function(m,l){i._listen(j,l)})});this.trigger("run",{start_url:h});this._running=true;this.$element().data(this.data_store_name,this);this.last_location=null;if(this.getLocation()==""&&typeof h!="undefined"){this.setLocation(h)}this._checkLocation();this.location_proxy.bind();this.bind("location-changed",function(){i._checkLocation()});this.bind("submit",function(k){var j=i._checkFormSubmission(g(k.target).closest("form"));return(j===false)?k.preventDefault():false});g("body").bind("onunload",function(){i.unload()});return this.trigger("changed")},unload:function(){if(!this.isRunning()){return false}var h=this;this.trigger("unload");this.location_proxy.unbind();this.$element().unbind("submit").removeClass(h.eventNamespace());this.$element().removeData(this.data_store_name);g.each(this.listeners.toHash(),function(i,j){g.each(j,function(l,k){h._unlisten(i,k)})});this._running=false;return this},bindToAllEvents:function(i){var h=this;g.each(this.APP_EVENTS,function(j,k){h.bind(k,i)});g.each(this.listeners.keys(true),function(k,j){if(h.APP_EVENTS.indexOf(j)==-1){h.bind(j,i)}});return this},routablePath:function(h){return h.replace(c,"")},lookupRoute:function(k,i){var j=this,h=false;this.trigger("lookup-route",{verb:k,path:i});if(typeof this.routes[k]!="undefined"){g.each(this.routes[k],function(m,l){if(j.routablePath(i).match(l.path)){h=l;return false}})}return h},runRoute:function(i,r,k){this.log("runRoute",[i,r].join(" "));this.trigger("run-route",{verb:i,path:r,params:k});if(typeof k=="undefined"){k={}}g.extend(k,this._parseQueryString(r));var j=this,h,n,l,m,q,o,p=this.lookupRoute(i,r);if(p){this.trigger("route-found",{route:p});if((path_params=p.path.exec(this.routablePath(r)))!=null){path_params.shift();g.each(path_params,function(s,t){if(p.param_names[s]){k[p.param_names[s]]=a(t)}else{if(!k.splat){k.splat=[]}k.splat.push(a(t))}})}h=new this.context_prototype(this,i,r,k);l=this.arounds.slice(0);q=this.befores.slice(0);n=function(){var s;while(q.length>0){o=q.shift();if(j.contextMatchesOptions(h,o[0])){s=o[1].apply(h,[h]);if(s===false){return false}}}j.last_route=p;h.trigger("event-context-before",{context:h});s=p.callback.apply(h,[h]);h.trigger("event-context-after",{context:h});return s};g.each(l.reverse(),function(s,t){var u=n;n=function(){return t.apply(h,[u])}});return n()}else{this.notFound(i,r)}},contextMatchesOptions:function(k,m,i){var j=m;if(typeof j==="undefined"||j=={}){return true}if(typeof i==="undefined"){i=true}if(typeof j==="string"||g.isFunction(j.test)){j={path:j}}if(j.only){return this.contextMatchesOptions(k,j.only,true)}else{if(j.except){return this.contextMatchesOptions(k,j.except,false)}}var h=true,l=true;if(j.path){if(g.isFunction(j.path.test)){h=j.path.test(k.path)}else{h=(j.path.toString()===k.path)}}if(j.verb){l=j.verb===k.verb}return i?(l&&h):!(l&&h)},getLocation:function(){return this.location_proxy.getLocation()},setLocation:function(h){return this.location_proxy.setLocation(h)},swap:function(h){return this.$element().html(h)},notFound:function(i,h){this.trigger("error-404",{verb:i,path:h});throw ("404 Not Found "+i+" "+h)},_checkLocation:function(){try{var h,i;h=this.getLocation();if(h!=this.last_location){i=this.runRoute("get",h)}this.last_location=h}catch(j){this.last_location=h;if(j.toString().match(/^404/)&&this.silence_404){return i}else{throw (j)}}return i},_checkFormSubmission:function(j){var h,l,n,m,i;this.trigger("check-form-submission",{form:j});h=g(j);l=h.attr("action");n=g.trim(h.attr("method").toString().toLowerCase());if(!n||n==""){n="get"}this.log("_checkFormSubmission",h,l,n);m=g.extend({},this._parseFormParams(h),{"$form":h});try{i=this.runRoute(n,l,m)}catch(k){if(k.toString().match(/^404/)&&this.silence_404){return true}else{throw (k)}}return(typeof i=="undefined")?false:i},_parseFormParams:function(h){var i={};g.each(h.serializeArray(),function(j,k){if(i[k.name]){if(g.isArray(i[k.name])){i[k.name].push(k.value)}else{i[k.name]=[i[k.name],k.value]}}else{i[k.name]=k.value}});return i},_parseQueryString:function(m){var k={},l,j,n,h;l=m.match(c);if(l){j=l[1].split("&");for(h=0;h<j.length;h+=1){n=j[h].split("=");k[n[0]]=a(n[1])}}return k},_listen:function(h,i){return this.$element().bind([h,this.eventNamespace()].join("."),i)},_unlisten:function(h,i){return this.$element().unbind([h,this.eventNamespace()].join("."),i)}});Sammy.EventContext=function(k,j,h,i){this.app=k;this.verb=j;this.path=h;this.params=new Sammy.Object(i)};Sammy.EventContext.prototype=g.extend({},Sammy.Object.prototype,{$element:function(){return this.app.$element()},partial:function(p,l,o){var h,k,n,m,j="partial:"+p,i=this;if((n=p.match(/\.([^\.]+)$/))){n=n[1]}if((!n||!g.isFunction(i[n]))&&this.app.template_engine){n=this.app.template_engine}if(n&&!g.isFunction(n)&&g.isFunction(i[n])){n=i[n]}if(!o&&g.isFunction(l)){o=l;l={}}m=(g.isArray(l)?l:[l||{}]),k=function(q){var r=q,s="";g.each(m,function(t,u){g.extend(u,i);if(g.isFunction(n)){r=n.apply(i,[q,u])}s+=r;if(o){return o.apply(i,[r,t])}});if(!o){i.app.swap(s)}i.trigger("changed")};if(this.app.cache_partials&&this.cache(j)){k.apply(i,[this.cache(j)])}else{g.get(p,function(q){if(i.app.cache_partials){i.cache(j,q)}k.apply(i,[q])})}},redirect:function(){var j,i=g.makeArray(arguments),h=this.app.getLocation();if(i.length>1){i.unshift("/");j=this.join.apply(this,i)}else{j=i[0]}this.trigger("redirect",{to:j});this.app.last_location=this.path;this.app.setLocation(j);if(h==j){this.app.trigger("location-changed")}},trigger:function(h,i){if(typeof i=="undefined"){i={}}if(!i.context){i.context=this}return this.app.trigger(h,i)},eventNamespace:function(){return this.app.eventNamespace()},notFound:function(){return this.app.notFound(this.verb,this.path)},toString:function(){return"Sammy.EventContext: "+[this.verb,this.path,this.params].join(" ")}});g.sammy=Sammy})(jQuery);
|
@@ -2,4 +2,4 @@
|
|
2
2
|
// http://code.quirkey.com/sammy
|
3
3
|
// Version: 0.5.0
|
4
4
|
// Built: Mon Feb 15 14:13:25 -0500 2010
|
5
|
-
(function(g){var d="([^/]+)",f=/:([\w\d]+)/g,c=/\?([^#]*)$/,a=decodeURIComponent,b=function(h){return function(i,j){return this.route.apply(this,[h,i,j])}},e=[];Sammy=function(){var i=g.makeArray(arguments),j,h;Sammy.apps=Sammy.apps||{};if(i.length==0||i[0]&&g.isFunction(i[0])){return Sammy.apply(Sammy,["body"].concat(i))}else{if(typeof(h=i.shift())=="string"){j=Sammy.apps[h]||new Sammy.Application();j.element_selector=h;if(i.length>0){g.each(i,function(k,l){j.use(l)})}if(j.element_selector!=h){delete Sammy.apps[h]}Sammy.apps[j.element_selector]=j;return j}}};Sammy.VERSION="0.5.0";Sammy.addLogger=function(h){e.push(h)};Sammy.log=function(){var h=g.makeArray(arguments);h.unshift("["+Date()+"]");g.each(e,function(k,j){j.apply(Sammy,h)})};if(typeof window.console!="undefined"){if(g.isFunction(console.log.apply)){Sammy.addLogger(function(){window.console.log.apply(console,arguments)})}else{Sammy.addLogger(function(){window.console.log(arguments)})}}else{if(typeof console!="undefined"){Sammy.addLogger(function(){console.log.apply(console,arguments)})}}Sammy.Object=function(h){return g.extend(this,h||{})};g.extend(Sammy.Object.prototype,{toHash:function(){var h={};g.each(this,function(j,i){if(!g.isFunction(i)){h[j]=i}});return h},toHTML:function(){var h="";g.each(this,function(j,i){if(!g.isFunction(i)){h+="<strong>"+j+"</strong> "+i+"<br />"}});return h},uuid:function(){if(typeof this._uuid=="undefined"||!this._uuid){this._uuid=(new Date()).getTime()+"-"+parseInt(Math.random()*1000)}return this._uuid},keys:function(h){var i=[];for(var j in this){if(!g.isFunction(this[j])||!h){i.push(j)}}return i},has:function(h){return this[h]&&g.trim(this[h].toString())!=""},join:function(){var i=g.makeArray(arguments);var h=i.shift();return i.join(h)},log:function(){Sammy.log.apply(Sammy,arguments)},toString:function(h){var i=[];g.each(this,function(l,j){if(!g.isFunction(j)||h){i.push('"'+l+'": '+j.toString())}});return"Sammy.Object: {"+i.join(",")+"}"}});Sammy.HashLocationProxy=function(i,h){this.app=i;if("onhashchange" in window){Sammy.log("native hash change exists, using");this.is_native=true}else{Sammy.log("no native hash change, falling back to polling");this.is_native=false;this._startPolling(h)}};Sammy.HashLocationProxy.prototype={bind:function(){var h=this.app;g(window).bind("hashchange."+this.app.eventNamespace(),function(){h.trigger("location-changed")})},unbind:function(){g(window).die("hashchange."+this.app.eventNamespace())},getLocation:function(){var h=window.location.toString().match(/^[^#]*(#.+)$/);return h?h[1]:""},setLocation:function(h){return window.location=h},_startPolling:function(j){var i=this;if(!Sammy.HashLocationProxy._interval){if(!j){j=10}var h=function(){current_location=i.getLocation();if(!Sammy.HashLocationProxy._last_location||current_location!=Sammy.HashLocationProxy._last_location){setTimeout(function(){g(window).trigger("hashchange")},1)}Sammy.HashLocationProxy._last_location=current_location};h();Sammy.HashLocationProxy._interval=setInterval(h,j);g(window).bind("unload",function(){clearInterval(Sammy.HashLocationProxy._interval)})}}};Sammy.DataLocationProxy=function(i,h){this.app=i;this.data_name=h||"sammy-location"};Sammy.DataLocationProxy.prototype={bind:function(){var h=this;this.app.$element().bind("setData",function(j,i){if(i==h.data_name){h.app.trigger("location-changed")}})},unbind:function(){this.app.$element().die("setData")},getLocation:function(){return this.app.$element().data(this.data_name)},setLocation:function(h){return this.app.$element().data(this.data_name,h)}};Sammy.Application=function(h){var i=this;this.routes={};this.listeners=new Sammy.Object({});this.arounds=[];this.befores=[];this.namespace=this.uuid();this.context_prototype=function(){Sammy.EventContext.apply(this,arguments)};this.context_prototype.prototype=new Sammy.EventContext();if(g.isFunction(h)){h.apply(this,[this])}if(!this.location_proxy){this.location_proxy=new Sammy.HashLocationProxy(i,this.run_interval_every)}if(this.debug){this.bindToAllEvents(function(k,j){i.log(i.toString(),k.cleaned_type,j||{})})}};Sammy.Application.prototype=g.extend({},Sammy.Object.prototype,{ROUTE_VERBS:["get","post","put","delete"],APP_EVENTS:["run","unload","lookup-route","run-route","route-found","event-context-before","event-context-after","changed","error-404","check-form-submission","redirect"],_last_route:null,_running:false,data_store_name:"sammy-app",element_selector:"body",debug:false,silence_404:true,run_interval_every:50,location_proxy:null,template_engine:null,toString:function(){return"Sammy.Application:"+this.element_selector},$element:function(){return g(this.element_selector)},use:function(){var h=g.makeArray(arguments);var i=h.shift();try{h.unshift(this);i.apply(this,h)}catch(j){if(typeof i=="undefined"){throw ("Error: called use() but plugin is not defined")}else{if(!g.isFunction(i)){throw ("Error: called use() but '"+i.toString()+"' is not a function")}else{throw (j)}}}return this},route:function(k,i,m){var j=this,l=[],h;if(!m&&g.isFunction(i)){i=k;m=i;k="any"}k=k.toLowerCase();if(i.constructor==String){f.lastIndex=0;while((path_match=f.exec(i))!=null){l.push(path_match[1])}i=new RegExp("^"+i.replace(f,d)+"$")}if(typeof m=="string"){m=j[m]}h=function(n){var o={verb:n,path:i,callback:m,param_names:l};j.routes[n]=j.routes[n]||[];j.routes[n].push(o)};if(k==="any"){g.each(this.ROUTE_VERBS,function(o,n){h(n)})}else{h(k)}return this},get:b("get"),post:b("post"),put:b("put"),del:b("delete"),any:b("any"),mapRoutes:function(i){var h=this;g.each(i,function(j,k){h.route.apply(h,k)});return this},eventNamespace:function(){return[this.data_store_name,this.namespace].join("-")},bind:function(h,j,l){var k=this;if(typeof l=="undefined"){l=j}var i=function(){var o,m,n;o=arguments[0];n=arguments[1];if(n&&n.context){m=n.context;delete n.context}else{m=new k.context_prototype(k,"bind",o.type,n)}o.cleaned_type=o.type.replace(k.eventNamespace(),"");l.apply(m,[o,n])};if(!this.listeners[h]){this.listeners[h]=[]}this.listeners[h].push(i);if(this.isRunning()){this._listen(h,i)}return this},trigger:function(h,i){this.$element().trigger([h,this.eventNamespace()].join("."),[i]);return this},refresh:function(){this.last_location=null;this.trigger("location-changed");return this},before:function(h,i){if(g.isFunction(h)){i=h;h={}}this.befores.push([h,i]);return this},after:function(h){return this.bind("event-context-after",h)},around:function(h){this.arounds.push(h);return this},isRunning:function(){return this._running},helpers:function(h){g.extend(this.context_prototype.prototype,h);return this},helper:function(h,i){this.context_prototype.prototype[h]=i;return this},run:function(h){if(this.isRunning()){return false}var i=this;g.each(this.listeners.toHash(),function(j,k){g.each(k,function(m,l){i._listen(j,l)})});this.trigger("run",{start_url:h});this._running=true;this.$element().data(this.data_store_name,this);this.last_location=null;if(this.getLocation()==""&&typeof h!="undefined"){this.setLocation(h)}this._checkLocation();this.location_proxy.bind();this.bind("location-changed",function(){i._checkLocation()});this.bind("submit",function(k){var j=i._checkFormSubmission(g(k.target).closest("form"));return(j===false)?k.preventDefault():false});g("body").bind("onunload",function(){i.unload()});return this.trigger("changed")},unload:function(){if(!this.isRunning()){return false}var h=this;this.trigger("unload");this.location_proxy.unbind();this.$element().unbind("submit").removeClass(h.eventNamespace());this.$element().removeData(this.data_store_name);g.each(this.listeners.toHash(),function(i,j){g.each(j,function(l,k){h._unlisten(i,k)})});this._running=false;return this},bindToAllEvents:function(i){var h=this;g.each(this.APP_EVENTS,function(j,k){h.bind(k,i)});g.each(this.listeners.keys(true),function(k,j){if(h.APP_EVENTS.indexOf(j)==-1){h.bind(j,i)}});return this},routablePath:function(h){return h.replace(c,"")},lookupRoute:function(k,i){var j=this,h=false;this.trigger("lookup-route",{verb:k,path:i});if(typeof this.routes[k]!="undefined"){g.each(this.routes[k],function(m,l){if(j.routablePath(i).match(l.path)){h=l;return false}})}return h},runRoute:function(i,r,k){this.log("runRoute",[i,r].join(" "));this.trigger("run-route",{verb:i,path:r,params:k});if(typeof k=="undefined"){k={}}g.extend(k,this._parseQueryString(r));var j=this,h,n,l,m,q,o,p=this.lookupRoute(i,r);if(p){this.trigger("route-found",{route:p});if((path_params=p.path.exec(this.routablePath(r)))!=null){path_params.shift();g.each(path_params,function(s,t){if(p.param_names[s]){k[p.param_names[s]]=a(t)}else{if(!k.splat){k.splat=[]}k.splat.push(a(t))}})}h=new this.context_prototype(this,i,r,k);l=this.arounds.slice(0);q=this.befores.slice(0);n=function(){var s;while(q.length>0){o=q.shift();if(j.contextMatchesOptions(h,o[0])){s=o[1].apply(h,[h]);if(s===false){return false}}}j.last_route=p;h.trigger("event-context-before",{context:h});s=p.callback.apply(h,[h]);h.trigger("event-context-after",{context:h});return s};g.each(l.reverse(),function(s,t){var u=n;n=function(){return t.apply(h,[u])}});return n()}else{this.notFound(i,r)}},contextMatchesOptions:function(k,m,i){var j=m;if(typeof j==="undefined"||j=={}){return true}if(typeof i==="undefined"){i=true}if(typeof j==="string"||g.isFunction(j.test)){j={path:j}}if(j.only){return this.contextMatchesOptions(k,j.only,true)}else{if(j.except){return this.contextMatchesOptions(k,j.except,false)}}var h=true,l=true;if(j.path){if(g.isFunction(j.path.test)){h=j.path.test(k.path)}else{h=(j.path.toString()===k.path)}}if(j.verb){l=j.verb===k.verb}return i?(l&&h):!(l&&h)},getLocation:function(){return this.location_proxy.getLocation()},setLocation:function(h){return this.location_proxy.setLocation(h)},swap:function(h){return this.$element().html(h)},notFound:function(i,h){this.trigger("error-404",{verb:i,path:h});throw ("404 Not Found "+i+" "+h)},_checkLocation:function(){try{var h,i;h=this.getLocation();if(h!=this.last_location){i=this.runRoute("get",h)}this.last_location=h}catch(j){this.last_location=h;if(j.toString().match(/^404/)&&this.silence_404){return i}else{throw (j)}}return i},_checkFormSubmission:function(j){var h,l,n,m,i;this.trigger("check-form-submission",{form:j});h=g(j);l=h.attr("action");n=g.trim(h.attr("method").toString().toLowerCase());if(!n||n==""){n="get"}this.log("_checkFormSubmission",h,l,n);m=g.extend({},this._parseFormParams(h),{"$form":h});try{i=this.runRoute(n,l,m)}catch(k){if(k.toString().match(/^404/)&&this.silence_404){return true}else{throw (k)}}return(typeof i=="undefined")?false:i},_parseFormParams:function(h){var i={};g.each(h.serializeArray(),function(j,k){if(i[k.name]){if(g.isArray(i[k.name])){i[k.name].push(k.value)}else{i[k.name]=[i[k.name],k.value]}}else{i[k.name]=k.value}});return i},_parseQueryString:function(m){var k={},l,j,n,h;l=m.match(c);if(l){j=l[1].split("&");for(h=0;h<j.length;h+=1){n=j[h].split("=");k[n[0]]=a(n[1])}}return k},_listen:function(h,i){return this.$element().bind([h,this.eventNamespace()].join("."),i)},_unlisten:function(h,i){return this.$element().unbind([h,this.eventNamespace()].join("."),i)}});Sammy.EventContext=function(k,j,h,i){this.app=k;this.verb=j;this.path=h;this.params=new Sammy.Object(i)};Sammy.EventContext.prototype=g.extend({},Sammy.Object.prototype,{$element:function(){return this.app.$element()},partial:function(p,l,o){var h,k,n,m,j="partial:"+p,i=this;if((n=p.match(/\.([^\.]+)$/))){n=n[1]}if((!n||!g.isFunction(i[n]))&&this.app.template_engine){n=this.app.template_engine}if(n&&!g.isFunction(n)&&g.isFunction(i[n])){n=i[n]}if(!o&&g.isFunction(l)){o=l;l={}}m=(g.isArray(l)?l:[l||{}]),k=function(q){var r=q,s="";g.each(m,function(t,u){g.extend(u,i);if(g.isFunction(n)){r=n.apply(i,[q,u])}s+=r;if(o){return o.apply(i,[r,t])}});if(!o){i.app.swap(s)}i.trigger("changed")};if(this.app.cache_partials&&this.cache(j)){k.apply(i,[this.cache(j)])}else{g.get(p,function(q){if(i.app.cache_partials){i.cache(j,q)}k.apply(i,[q])})}},redirect:function(){var j,i=g.makeArray(arguments),h=this.app.getLocation();if(i.length>1){i.unshift("/");j=this.join.apply(this,i)}else{j=i[0]}this.trigger("redirect",{to:j});this.app.last_location=this.path;this.app.setLocation(j);if(h==j){this.app.trigger("location-changed")}},trigger:function(h,i){if(typeof i=="undefined"){i={}}if(!i.context){i.context=this}return this.app.trigger(h,i)},eventNamespace:function(){return this.app.eventNamespace()},notFound:function(){return this.app.notFound(this.verb,this.path)},toString:function(){return"Sammy.EventContext: "+[this.verb,this.path,this.params].join(" ")}});g.sammy=Sammy})(jQuery);
|
5
|
+
(function(g){var d="([^/]+)",f=/:([\w\d]+)/g,c=/\?([^#]*)$/,a=decodeURIComponent,b=function(h){return function(i,j){return this.route.apply(this,[h,i,j])}},e=[];Sammy=function(){var i=g.makeArray(arguments),j,h;Sammy.apps=Sammy.apps||{};if(i.length==0||i[0]&&g.isFunction(i[0])){return Sammy.apply(Sammy,["body"].concat(i))}else{if(typeof(h=i.shift())=="string"){j=Sammy.apps[h]||new Sammy.Application();j.element_selector=h;if(i.length>0){g.each(i,function(k,l){j.use(l)})}if(j.element_selector!=h){delete Sammy.apps[h]}Sammy.apps[j.element_selector]=j;return j}}};Sammy.VERSION="0.5.0";Sammy.addLogger=function(h){e.push(h)};Sammy.log=function(){var h=g.makeArray(arguments);h.unshift("["+Date()+"]");g.each(e,function(k,j){j.apply(Sammy,h)})};if(typeof window.console!="undefined"){if(g.isFunction(console.log.apply)){Sammy.addLogger(function(){window.console.log.apply(console,arguments)})}else{Sammy.addLogger(function(){window.console.log(arguments)})}}else{if(typeof console!="undefined"){Sammy.addLogger(function(){console.log.apply(console,arguments)})}}Sammy.Object=function(h){return g.extend(this,h||{})};g.extend(Sammy.Object.prototype,{toHash:function(){var h={};g.each(this,function(j,i){if(!g.isFunction(i)){h[j]=i}});return h},toHTML:function(){var h="";g.each(this,function(j,i){if(!g.isFunction(i)){h+="<strong>"+j+"</strong> "+i+"<br />"}});return h},uuid:function(){if(typeof this._uuid=="undefined"||!this._uuid){this._uuid=(new Date()).getTime()+"-"+parseInt(Math.random()*1000)}return this._uuid},keys:function(h){var i=[];for(var j in this){if(!g.isFunction(this[j])||!h){i.push(j)}}return i},has:function(h){return this[h]&&g.trim(this[h].toString())!=""},join:function(){var i=g.makeArray(arguments);var h=i.shift();return i.join(h)},log:function(){Sammy.log.apply(Sammy,arguments)},toString:function(h){var i=[];g.each(this,function(l,j){if(!g.isFunction(j)||h){i.push('"'+l+'": '+j.toString())}});return"Sammy.Object: {"+i.join(",")+"}"}});Sammy.HashLocationProxy=function(i,h){this.app=i;if("onhashchange" in window){Sammy.log("native hash change exists, using");this.is_native=true}else{Sammy.log("no native hash change, falling back to polling");this.is_native=false;this._startPolling(h)}};Sammy.HashLocationProxy.prototype={bind:function(){var h=this.app;g(window).bind("hashchange."+this.app.eventNamespace(),function(){h.trigger("location-changed")})},unbind:function(){g(window).die("hashchange."+this.app.eventNamespace())},getLocation:function(){var h=window.location.toString().match(/^[^#]*(#.+)$/);return h?h[1]:""},setLocation:function(h){return window.location=h},_startPolling:function(j){var i=this;if(!Sammy.HashLocationProxy._interval){if(!j){j=10}var h=function(){current_location=i.getLocation();if(!Sammy.HashLocationProxy._last_location||current_location!=Sammy.HashLocationProxy._last_location){setTimeout(function(){g(window).trigger("hashchange")},1)}Sammy.HashLocationProxy._last_location=current_location};h();Sammy.HashLocationProxy._interval=setInterval(h,j);g(window).bind("unload",function(){clearInterval(Sammy.HashLocationProxy._interval)})}}};Sammy.DataLocationProxy=function(i,h){this.app=i;this.data_name=h||"sammy-location"};Sammy.DataLocationProxy.prototype={bind:function(){var h=this;this.app.$element().bind("setData",function(j,i){if(i==h.data_name){h.app.trigger("location-changed")}})},unbind:function(){this.app.$element().die("setData")},getLocation:function(){return this.app.$element().data(this.data_name)},setLocation:function(h){return this.app.$element().data(this.data_name,h)}};Sammy.Application=function(h){var i=this;this.routes={};this.listeners=new Sammy.Object({});this.arounds=[];this.befores=[];this.namespace=this.uuid();this.context_prototype=function(){Sammy.EventContext.apply(this,arguments)};this.context_prototype.prototype=new Sammy.EventContext();if(g.isFunction(h)){h.apply(this,[this])}if(!this.location_proxy){this.location_proxy=new Sammy.HashLocationProxy(i,this.run_interval_every)}if(this.debug){this.bindToAllEvents(function(k,j){i.log(i.toString(),k.cleaned_type,j||{})})}};Sammy.Application.prototype=g.extend({},Sammy.Object.prototype,{ROUTE_VERBS:["get","post","put","delete"],APP_EVENTS:["run","unload","lookup-route","run-route","route-found","event-context-before","event-context-after","changed","error-404","check-form-submission","redirect"],_last_route:null,_running:false,data_store_name:"sammy-app",element_selector:"body",debug:false,silence_404:true,run_interval_every:50,location_proxy:null,template_engine:null,toString:function(){return"Sammy.Application:"+this.element_selector},$element:function(){return g(this.element_selector)},use:function(){var h=g.makeArray(arguments);var i=h.shift();try{h.unshift(this);i.apply(this,h)}catch(j){if(typeof i=="undefined"){throw ("Error: called use() but plugin is not defined")}else{if(!g.isFunction(i)){throw ("Error: called use() but '"+i.toString()+"' is not a function")}else{throw (j)}}}return this},route:function(k,i,m){var j=this,l=[],h;if(!m&&g.isFunction(i)){i=k;m=i;k="any"}k=k.toLowerCase();if(i.constructor==String){f.lastIndex=0;while((path_match=f.exec(i))!=null){l.push(path_match[1])}i=new RegExp("^"+i.replace(f,d)+"$")}if(typeof m=="string"){m=j[m]}h=function(n){var o={verb:n,path:i,callback:m,param_names:l};j.routes[n]=j.routes[n]||[];j.routes[n].push(o)};if(k==="any"){g.each(this.ROUTE_VERBS,function(o,n){h(n)})}else{h(k)}return this},get:b("get"),post:b("post"),put:b("put"),del:b("delete"),any:b("any"),mapRoutes:function(i){var h=this;g.each(i,function(j,k){h.route.apply(h,k)});return this},eventNamespace:function(){return[this.data_store_name,this.namespace].join("-")},bind:function(h,j,l){var k=this;if(typeof l=="undefined"){l=j}var i=function(){var o,m,n;o=arguments[0];n=arguments[1];if(n&&n.context){m=n.context;delete n.context}else{m=new k.context_prototype(k,"bind",o.type,n)}o.cleaned_type=o.type.replace(k.eventNamespace(),"");l.apply(m,[o,n])};if(!this.listeners[h]){this.listeners[h]=[]}this.listeners[h].push(i);if(this.isRunning()){this._listen(h,i)}return this},trigger:function(h,i){this.$element().trigger([h,this.eventNamespace()].join("."),[i]);return this},refresh:function(){this.last_location=null;this.trigger("location-changed");return this},before:function(h,i){if(g.isFunction(h)){i=h;h={}}this.befores.push([h,i]);return this},after:function(h){return this.bind("event-context-after",h)},around:function(h){this.arounds.push(h);return this},isRunning:function(){return this._running},helpers:function(h){g.extend(this.context_prototype.prototype,h);return this},helper:function(h,i){this.context_prototype.prototype[h]=i;return this},run:function(h){if(this.isRunning()){return false}var i=this;g.each(this.listeners.toHash(),function(j,k){g.each(k,function(m,l){i._listen(j,l)})});this.trigger("run",{start_url:h});this._running=true;this.$element().data(this.data_store_name,this);this.last_location=null;if(this.getLocation()==""&&typeof h!="undefined"){this.setLocation(h)}this._checkLocation();this.location_proxy.bind();this.bind("location-changed",function(){i._checkLocation()});this.bind("submit",function(k){var j=i._checkFormSubmission(g(k.target).closest("form"));return(j===false)?k.preventDefault():false});g("body").bind("onunload",function(){i.unload()});return this.trigger("changed")},unload:function(){if(!this.isRunning()){return false}var h=this;this.trigger("unload");this.location_proxy.unbind();this.$element().unbind("submit").removeClass(h.eventNamespace());this.$element().removeData(this.data_store_name);g.each(this.listeners.toHash(),function(i,j){g.each(j,function(l,k){h._unlisten(i,k)})});this._running=false;return this},bindToAllEvents:function(i){var h=this;g.each(this.APP_EVENTS,function(j,k){h.bind(k,i)});g.each(this.listeners.keys(true),function(k,j){if(h.APP_EVENTS.indexOf(j)==-1){h.bind(j,i)}});return this},routablePath:function(h){return h.replace(c,"")},lookupRoute:function(k,i){var j=this,h=false;this.trigger("lookup-route",{verb:k,path:i});if(typeof this.routes[k]!="undefined"){g.each(this.routes[k],function(m,l){if(j.routablePath(i).match(l.path)){h=l;return false}})}return h},runRoute:function(i,r,k){this.log("runRoute",[i,r].join(" "));this.trigger("run-route",{verb:i,path:r,params:k});if(typeof k=="undefined"){k={}}g.extend(k,this._parseQueryString(r));var j=this,h,n,l,m,q,o,p=this.lookupRoute(i,r);if(p){this.trigger("route-found",{route:p});if((path_params=p.path.exec(this.routablePath(r)))!=null){path_params.shift();g.each(path_params,function(s,t){if(p.param_names[s]){k[p.param_names[s]]=a(t)}else{if(!k.splat){k.splat=[]}k.splat.push(a(t))}})}h=new this.context_prototype(this,i,r,k);l=this.arounds.slice(0);q=this.befores.slice(0);n=function(){var s;while(q.length>0){o=q.shift();if(j.contextMatchesOptions(h,o[0])){s=o[1].apply(h,[h]);if(s===false){return false}}}j.last_route=p;h.trigger("event-context-before",{context:h});s=p.callback.apply(h,[h]);h.trigger("event-context-after",{context:h});return s};g.each(l.reverse(),function(s,t){var u=n;n=function(){return t.apply(h,[u])}});return n()}else{this.notFound(i,r)}},contextMatchesOptions:function(k,m,i){var j=m;if(typeof j==="undefined"||j=={}){return true}if(typeof i==="undefined"){i=true}if(typeof j==="string"||g.isFunction(j.test)){j={path:j}}if(j.only){return this.contextMatchesOptions(k,j.only,true)}else{if(j.except){return this.contextMatchesOptions(k,j.except,false)}}var h=true,l=true;if(j.path){if(g.isFunction(j.path.test)){h=j.path.test(k.path)}else{h=(j.path.toString()===k.path)}}if(j.verb){l=j.verb===k.verb}return i?(l&&h):!(l&&h)},getLocation:function(){return this.location_proxy.getLocation()},setLocation:function(h){return this.location_proxy.setLocation(h)},swap:function(h){return this.$element().html(h)},notFound:function(i,h){this.trigger("error-404",{verb:i,path:h});throw ("404 Not Found "+i+" "+h)},_checkLocation:function(){try{var h,i;h=this.getLocation();if(h!=this.last_location){i=this.runRoute("get",h)}this.last_location=h}catch(j){this.last_location=h;if(j.toString().match(/^404/)&&this.silence_404){return i}else{throw (j)}}return i},_checkFormSubmission:function(j){var h,l,n,m,i;this.trigger("check-form-submission",{form:j});h=g(j);l=h.attr("action");n=g.trim(h.attr("method").toString().toLowerCase());if(!n||n==""){n="get"}this.log("_checkFormSubmission",h,l,n);m=g.extend({},this._parseFormParams(h),{"$form":h});try{i=this.runRoute(n,l,m)}catch(k){if(k.toString().match(/^404/)&&this.silence_404){return true}else{throw (k)}}return(typeof i=="undefined")?false:i},_parseFormParams:function(h){var i={};g.each(h.serializeArray(),function(j,k){if(i[k.name]){if(g.isArray(i[k.name])){i[k.name].push(k.value)}else{i[k.name]=[i[k.name],k.value]}}else{i[k.name]=k.value}});return i},_parseQueryString:function(m){var k={},l,j,n,h;l=m.match(c);if(l){j=l[1].split("&");for(h=0;h<j.length;h+=1){n=j[h].split("=");k[n[0]]=a(n[1])}}return k},_listen:function(h,i){return this.$element().bind([h,this.eventNamespace()].join("."),i)},_unlisten:function(h,i){return this.$element().unbind([h,this.eventNamespace()].join("."),i)}});Sammy.EventContext=function(k,j,h,i){this.app=k;this.verb=j;this.path=h;this.params=new Sammy.Object(i)};Sammy.EventContext.prototype=g.extend({},Sammy.Object.prototype,{$element:function(){return this.app.$element()},partial:function(p,l,o){var h,k,n,m,j="partial:"+p,i=this;if((n=p.match(/\.([^\.]+)$/))){n=n[1]}if((!n||!g.isFunction(i[n]))&&this.app.template_engine){n=this.app.template_engine}if(n&&!g.isFunction(n)&&g.isFunction(i[n])){n=i[n]}if(!o&&g.isFunction(l)){o=l;l={}}m=(g.isArray(l)?l:[l||{}]),k=function(q){var r=q,s="";g.each(m,function(t,u){g.extend(u,i);if(g.isFunction(n)){r=n.apply(i,[q,u])}s+=r;if(o){return o.apply(i,[r,t])}});if(!o){i.app.swap(s)}i.trigger("changed")};if(this.app.cache_partials&&this.cache(j)){k.apply(i,[this.cache(j)])}else{g.get(p,function(q){if(i.app.cache_partials){i.cache(j,q)}k.apply(i,[q])})}},redirect:function(){var j,i=g.makeArray(arguments),h=this.app.getLocation();if(i.length>1){i.unshift("/");j=this.join.apply(this,i)}else{j=i[0]}this.trigger("redirect",{to:j});this.app.last_location=this.path;this.app.setLocation(j);if(h==j){this.app.trigger("location-changed")}},trigger:function(h,i){if(typeof i=="undefined"){i={}}if(!i.context){i.context=this}return this.app.trigger(h,i)},eventNamespace:function(){return this.app.eventNamespace()},notFound:function(){return this.app.notFound(this.verb,this.path)},toString:function(){return"Sammy.EventContext: "+[this.verb,this.path,this.params].join(" ")}});g.sammy=Sammy})(jQuery);
|
@@ -5,7 +5,7 @@
|
|
5
5
|
|
6
6
|
Sammy = Sammy || {};
|
7
7
|
|
8
|
-
// A simple cache strategy that stores key/values in memory.
|
8
|
+
// A simple cache strategy that stores key/values in memory.
|
9
9
|
Sammy.MemoryCacheProxy = function(initial) {
|
10
10
|
this._cache = initial || {};
|
11
11
|
};
|
@@ -24,8 +24,8 @@
|
|
24
24
|
delete this._cache[name];
|
25
25
|
}
|
26
26
|
});
|
27
|
-
|
28
|
-
// A simple cache strategy that stores key/values <tt>$element.data()</tt> with a <tt>cache.</tt> prefix
|
27
|
+
|
28
|
+
// A simple cache strategy that stores key/values <tt>$element.data()</tt> with a <tt>cache.</tt> prefix
|
29
29
|
Sammy.DataCacheProxy = function(initial, $element) {
|
30
30
|
initial = initial || {};
|
31
31
|
this.$element = $element;
|
@@ -33,7 +33,7 @@
|
|
33
33
|
$element.data('cache.' + key, value);
|
34
34
|
});
|
35
35
|
};
|
36
|
-
|
36
|
+
|
37
37
|
$.extend(Sammy.DataCacheProxy.prototype, {
|
38
38
|
exists: function(name) {
|
39
39
|
return (typeof this.$element.data('cache.' + name) != "undefined");
|
@@ -49,26 +49,26 @@
|
|
49
49
|
}
|
50
50
|
});
|
51
51
|
|
52
|
-
// Sammy.Cache provides helpers for caching data within the lifecycle of a
|
52
|
+
// Sammy.Cache provides helpers for caching data within the lifecycle of a
|
53
53
|
// Sammy app. The plugin provides two main methods on <tt>Sammy.Application<tt>,
|
54
54
|
// <tt>cache</tt> and <tt>clearCache</tt>. Each app has its own cache store so that
|
55
55
|
// you dont have to worry about collisions. There are currently two different 'cache proxies'
|
56
|
-
// that share the same API but store the data in different ways.
|
56
|
+
// that share the same API but store the data in different ways.
|
57
57
|
//
|
58
58
|
// === Arguments
|
59
59
|
//
|
60
|
-
// +proxy+:: decides which caching proxy to use, either 'memory'(default) or 'data'
|
60
|
+
// +proxy+:: decides which caching proxy to use, either 'memory'(default) or 'data'
|
61
61
|
//
|
62
62
|
Sammy.Cache = function(app, proxy) {
|
63
|
-
|
63
|
+
|
64
64
|
app.log('**WARNING:** This version of Sammy.Cache has been deprecated in favor of using the version in Sammy.Storage and will be removed in 1.0')
|
65
65
|
|
66
66
|
if (proxy == 'data') {
|
67
67
|
this.cache_proxy = new Sammy.DataCacheProxy({}, this.$element());
|
68
68
|
} else {
|
69
69
|
this.cache_proxy = new Sammy.MemoryCacheProxy({});
|
70
|
-
}
|
71
|
-
|
70
|
+
}
|
71
|
+
|
72
72
|
app.cache_partials = true;
|
73
73
|
|
74
74
|
$.extend(app, {
|
@@ -89,7 +89,7 @@
|
|
89
89
|
// // this is the app
|
90
90
|
// return app.element_selector;
|
91
91
|
// });
|
92
|
-
//
|
92
|
+
//
|
93
93
|
cache: function(name, value) {
|
94
94
|
if (typeof value == 'undefined') {
|
95
95
|
return this.cache_proxy.get(name);
|
@@ -105,7 +105,7 @@
|
|
105
105
|
return this.cache_proxy.clear(name);
|
106
106
|
}
|
107
107
|
});
|
108
|
-
|
108
|
+
|
109
109
|
app.helpers({
|
110
110
|
// a helper shortcut for use in <tt>Sammy.EventContext</tt>
|
111
111
|
cache: function(name, value) {
|
@@ -114,4 +114,4 @@
|
|
114
114
|
});
|
115
115
|
};
|
116
116
|
|
117
|
-
})(jQuery);
|
117
|
+
})(jQuery);
|
@@ -510,7 +510,7 @@
|
|
510
510
|
//
|
511
511
|
Sammy.Haml = function(app, method_alias) {
|
512
512
|
app.use(Sammy.JSON);
|
513
|
-
|
513
|
+
|
514
514
|
var haml_cache = {};
|
515
515
|
// *Helper*:: Uses haml-js to parse a template and interpolate and work with the passed data
|
516
516
|
//
|
@@ -536,4 +536,4 @@
|
|
536
536
|
|
537
537
|
};
|
538
538
|
|
539
|
-
})(jQuery);
|
539
|
+
})(jQuery);
|
@@ -320,34 +320,34 @@
|
|
320
320
|
};
|
321
321
|
}
|
322
322
|
}());
|
323
|
-
|
323
|
+
|
324
324
|
Sammy = Sammy || {};
|
325
|
-
|
326
|
-
// Sammy.JSON is a simple wrapper around Douglas Crockford's ever-useful json2.js
|
327
|
-
// (http://www.json.org/js.html]) Sammy.JSON includes the top level JSON object if
|
328
|
-
// it doesn't already exist (a.k.a. does not override the native implementation that
|
329
|
-
// some browsers include). It also adds a <tt>json()</tt> helper to a Sammy app when
|
325
|
+
|
326
|
+
// Sammy.JSON is a simple wrapper around Douglas Crockford's ever-useful json2.js
|
327
|
+
// (http://www.json.org/js.html]) Sammy.JSON includes the top level JSON object if
|
328
|
+
// it doesn't already exist (a.k.a. does not override the native implementation that
|
329
|
+
// some browsers include). It also adds a <tt>json()</tt> helper to a Sammy app when
|
330
330
|
// included.
|
331
331
|
Sammy.JSON = function(app) {
|
332
|
-
|
333
|
-
app.helpers({
|
332
|
+
|
333
|
+
app.helpers({
|
334
334
|
// json is a polymorphic function that translates objects aback and forth
|
335
335
|
// from JSON to JS. If given a string, it will parse into JS, if given a JS
|
336
336
|
// object it will stringify into JSON.
|
337
337
|
//
|
338
338
|
// === Example
|
339
|
-
//
|
339
|
+
//
|
340
340
|
// var app = $.sammy(function() {
|
341
341
|
// this.use(Sammy.JSON);
|
342
|
-
//
|
342
|
+
//
|
343
343
|
// this.get('#/', function() {
|
344
344
|
// this.json({user_id: 123}); //=> "{\"user_id\":\"123\"}"
|
345
345
|
// this.json("{\"user_id\":\"123\"}"); //=> [object Object]
|
346
346
|
// this.json("{\"user_id\":\"123\"}").user_id; //=> "123"
|
347
347
|
// });
|
348
348
|
// })
|
349
|
-
//
|
350
|
-
//
|
349
|
+
//
|
350
|
+
//
|
351
351
|
json: function(object) {
|
352
352
|
if (typeof object == 'string') {
|
353
353
|
return JSON.parse(object);
|
@@ -356,7 +356,7 @@
|
|
356
356
|
}
|
357
357
|
}
|
358
358
|
});
|
359
|
-
|
359
|
+
|
360
360
|
}
|
361
|
-
|
362
|
-
})(jQuery);
|
361
|
+
|
362
|
+
})(jQuery);
|
@@ -4,7 +4,7 @@ if (!Mustache) {
|
|
4
4
|
|
5
5
|
/*
|
6
6
|
Shamless port of http://github.com/defunkt/mustache
|
7
|
-
by Jan Lehnardt <jan@apache.org>,
|
7
|
+
by Jan Lehnardt <jan@apache.org>,
|
8
8
|
Alexander Lang <alex@upstream-berlin.com>,
|
9
9
|
Sebastian Cohnen <sebastian.cohnen@googlemail.com>
|
10
10
|
|
@@ -297,52 +297,52 @@ if (!Mustache) {
|
|
297
297
|
}
|
298
298
|
});
|
299
299
|
}();
|
300
|
-
|
300
|
+
|
301
301
|
} // Ensure Mustache
|
302
302
|
|
303
303
|
Sammy = Sammy || {};
|
304
|
-
|
304
|
+
|
305
305
|
// <tt>Sammy.Mustache</tt> provides a quick way of using mustache style templates in your app.
|
306
306
|
// The plugin itself includes the awesome mustache.js lib created and maintained by Jan Lehnardt
|
307
307
|
// at http://github.com/janl/mustache.js
|
308
|
-
//
|
308
|
+
//
|
309
309
|
// Mustache is a clever templating system that relys on double brackets {{}} for interpolation.
|
310
310
|
// For full details on syntax check out the original Ruby implementation created by Chris Wanstrath at
|
311
311
|
// http://github.com/defunkt/mustache
|
312
|
-
//
|
313
|
-
// By default using Sammy.Mustache in your app adds the <tt>mustache()</tt> method to the EventContext
|
312
|
+
//
|
313
|
+
// By default using Sammy.Mustache in your app adds the <tt>mustache()</tt> method to the EventContext
|
314
314
|
// prototype. However, just like <tt>Sammy.Template</tt> you can change the default name of the method
|
315
|
-
// by passing a second argument (e.g. you could use the ms() as the method alias so that all the template
|
315
|
+
// by passing a second argument (e.g. you could use the ms() as the method alias so that all the template
|
316
316
|
// files could be in the form file.ms instead of file.mustache)
|
317
|
-
//
|
317
|
+
//
|
318
318
|
// === Example #1
|
319
|
-
//
|
319
|
+
//
|
320
320
|
// The template (mytemplate.ms):
|
321
|
-
//
|
321
|
+
//
|
322
322
|
// <h1>\{\{title\}\}<h1>
|
323
|
-
//
|
323
|
+
//
|
324
324
|
// Hey, {{name}}! Welcome to Mustache!
|
325
|
-
//
|
325
|
+
//
|
326
326
|
// The app:
|
327
|
-
//
|
327
|
+
//
|
328
328
|
// var $.app = $.sammy(function() {
|
329
329
|
// // include the plugin and alias mustache() to ms()
|
330
330
|
// this.use(Sammy.Mustache, 'ms');
|
331
|
-
//
|
331
|
+
//
|
332
332
|
// this.get('#/hello/:name', function() {
|
333
333
|
// // set local vars
|
334
334
|
// this.title = 'Hello!'
|
335
335
|
// this.name = this.params.name;
|
336
336
|
// // render the template and pass it through mustache
|
337
|
-
// this.partial('mytemplate.ms');
|
337
|
+
// this.partial('mytemplate.ms');
|
338
338
|
// });
|
339
|
-
//
|
339
|
+
//
|
340
340
|
// });
|
341
|
-
//
|
341
|
+
//
|
342
342
|
// If I go to #/hello/AQ in the browser, Sammy will render this to the <tt>body</tt>:
|
343
|
-
//
|
343
|
+
//
|
344
344
|
// <h1>Hello!</h1>
|
345
|
-
//
|
345
|
+
//
|
346
346
|
// Hey, AQ! Welcome to Mustache!
|
347
347
|
//
|
348
348
|
//
|
@@ -358,58 +358,58 @@ if (!Mustache) {
|
|
358
358
|
// Say hello to your friend {{friend}}!
|
359
359
|
//
|
360
360
|
// The app:
|
361
|
-
//
|
361
|
+
//
|
362
362
|
// var $.app = $.sammy(function() {
|
363
363
|
// // include the plugin and alias mustache() to ms()
|
364
364
|
// this.use(Sammy.Mustache, 'ms');
|
365
|
-
//
|
365
|
+
//
|
366
366
|
// this.get('#/hello/:name/to/:friend', function() {
|
367
|
-
|
368
|
-
|
369
|
-
|
370
|
-
|
371
|
-
|
372
|
-
|
373
|
-
|
374
|
-
|
375
|
-
|
376
|
-
|
377
|
-
|
378
|
-
|
367
|
+
// var context = this;
|
368
|
+
//
|
369
|
+
// // fetch mustache-partial first
|
370
|
+
// $.get('mypartial.ms', function(response){
|
371
|
+
// context.partials = response;
|
372
|
+
//
|
373
|
+
// // set local vars
|
374
|
+
// context.name = this.params.name;
|
375
|
+
// context.hello_friend = {name: this.params.friend};
|
376
|
+
//
|
377
|
+
// // render the template and pass it through mustache
|
378
|
+
// context.partial('mytemplate.ms');
|
379
379
|
// });
|
380
380
|
// });
|
381
|
-
//
|
381
|
+
//
|
382
382
|
// });
|
383
|
-
//
|
383
|
+
//
|
384
384
|
// If I go to #/hello/AQ/to/dP in the browser, Sammy will render this to the <tt>body</tt>:
|
385
|
-
//
|
385
|
+
//
|
386
386
|
// Hey, AQ! Say hello to your friend dP!
|
387
|
-
//
|
387
|
+
//
|
388
388
|
// Note: You dont have to include the mustache.js file on top of the plugin as the plugin
|
389
389
|
// includes the full source.
|
390
|
-
//
|
390
|
+
//
|
391
391
|
Sammy.Mustache = function(app, method_alias) {
|
392
|
-
|
392
|
+
|
393
393
|
// *Helper*:: Uses Mustache.js to parse a template and interpolate and work with the passed data
|
394
394
|
//
|
395
395
|
// === Arguments
|
396
|
-
//
|
396
|
+
//
|
397
397
|
// +template+:: A String template. {{}} Tags are evaluated and interpolated by Mustache.js
|
398
|
-
// +data+:: An Object containing the replacement values for the template.
|
398
|
+
// +data+:: An Object containing the replacement values for the template.
|
399
399
|
// data is extended with the <tt>EventContext</tt> allowing you to call its methods within the template.
|
400
400
|
// +partials+:: An Object containing one or more partials (String templates
|
401
401
|
// that are called from the main template).
|
402
402
|
//
|
403
403
|
var mustache = function(template, data, partials) {
|
404
404
|
data = $.extend({}, this, data);
|
405
|
-
partials = $.extend({}, data.partials, partials);
|
405
|
+
partials = $.extend({}, data.partials, partials);
|
406
406
|
return Mustache.to_html(template, data, partials);
|
407
407
|
};
|
408
|
-
|
408
|
+
|
409
409
|
// set the default method name/extension
|
410
|
-
if (!method_alias) method_alias = 'mustache';
|
410
|
+
if (!method_alias) method_alias = 'mustache';
|
411
411
|
app.helper(method_alias, mustache);
|
412
|
-
|
412
|
+
|
413
413
|
};
|
414
414
|
|
415
|
-
})(jQuery);
|
415
|
+
})(jQuery);
|
@@ -1,10 +1,10 @@
|
|
1
1
|
(function($) {
|
2
2
|
|
3
3
|
Sammy = Sammy || {};
|
4
|
-
|
4
|
+
|
5
5
|
function parseNestedParam(field_value, field_name, params) {
|
6
6
|
var match, name, rest;
|
7
|
-
|
7
|
+
|
8
8
|
if (field_name.match(/^[^\[]+$/)) {
|
9
9
|
// basic value
|
10
10
|
params[field_name] = unescape(field_value);
|
@@ -12,39 +12,39 @@
|
|
12
12
|
// array
|
13
13
|
name = match[1];
|
14
14
|
rest = match[2];
|
15
|
-
|
15
|
+
|
16
16
|
if(params[name] && !$.isArray(params[name])) { throw('400 Bad Request'); }
|
17
|
-
|
17
|
+
|
18
18
|
if (rest) {
|
19
19
|
// array is not at the end of the parameter string
|
20
20
|
match = rest.match(/^\[([^\]]+)\](.*)$/);
|
21
21
|
if(!match) { throw('400 Bad Request'); }
|
22
|
-
|
22
|
+
|
23
23
|
if (params[name]) {
|
24
24
|
if(params[name][params[name].length - 1][match[1]]) {
|
25
|
-
params[name].push(parseNestedParam(field_value, match[1] + match[2], {}));
|
25
|
+
params[name].push(parseNestedParam(field_value, match[1] + match[2], {}));
|
26
26
|
} else {
|
27
|
-
$.extend(true, params[name][params[name].length - 1], parseNestedParam(field_value, match[1] + match[2], {}));
|
28
|
-
}
|
27
|
+
$.extend(true, params[name][params[name].length - 1], parseNestedParam(field_value, match[1] + match[2], {}));
|
28
|
+
}
|
29
29
|
} else {
|
30
30
|
params[name] = [parseNestedParam(field_value, match[1] + match[2], {})];
|
31
|
-
}
|
31
|
+
}
|
32
32
|
} else {
|
33
33
|
// array is at the end of the parameter string
|
34
34
|
if (params[name]) {
|
35
35
|
params[name].push(unescape(field_value));
|
36
36
|
} else {
|
37
37
|
params[name] = [unescape(field_value)];
|
38
|
-
}
|
38
|
+
}
|
39
39
|
}
|
40
40
|
} else if (match = field_name.match(/^([^\[]+)\[([^\[]+)\](.*)$/)) {
|
41
41
|
// hash
|
42
42
|
name = match[1];
|
43
43
|
rest = match[2] + match[3];
|
44
|
-
|
44
|
+
|
45
45
|
if (params[name] && $.isArray(params[name])) { throw('400 Bad Request'); }
|
46
|
-
|
47
|
-
if (params[name]) {
|
46
|
+
|
47
|
+
if (params[name]) {
|
48
48
|
$.extend(true, params[name], parseNestedParam(field_value, rest, params[name]));
|
49
49
|
} else {
|
50
50
|
params[name] = parseNestedParam(field_value, rest, {});
|
@@ -53,38 +53,38 @@
|
|
53
53
|
return params;
|
54
54
|
};
|
55
55
|
|
56
|
-
|
56
|
+
|
57
57
|
// <tt>Sammy.NestedParams</tt> overrides the default form parsing behavior to provide
|
58
|
-
// extended functionality for parsing Rack/Rails style form name/value pairs into JS
|
58
|
+
// extended functionality for parsing Rack/Rails style form name/value pairs into JS
|
59
59
|
// Objects. In fact it passes the same suite of tests as Rack's nested query parsing.
|
60
60
|
// The code and tests were ported to JavaScript/Sammy by http://github.com/endor
|
61
|
-
//
|
61
|
+
//
|
62
62
|
// This allows you to translate a form with properly named inputs into a JSON object.
|
63
|
-
//
|
63
|
+
//
|
64
64
|
// === Example
|
65
|
-
//
|
65
|
+
//
|
66
66
|
// Given an HTML form like so:
|
67
|
-
//
|
67
|
+
//
|
68
68
|
// <form action="#/parse_me" method="post">
|
69
69
|
// <input type="text" name="obj[first]" />
|
70
70
|
// <input type="text" name="obj[second]" />
|
71
71
|
// <input type="text" name="obj[hash][first]" />
|
72
72
|
// <input type="text" name="obj[hash][second]" />
|
73
73
|
// </form>
|
74
|
-
//
|
74
|
+
//
|
75
75
|
// And a Sammy app like:
|
76
|
-
//
|
76
|
+
//
|
77
77
|
// var app = $.sammy(function(app) {
|
78
78
|
// this.use(Sammy.NestedParams);
|
79
|
-
//
|
79
|
+
//
|
80
80
|
// this.post('#/parse_me', function(context) {
|
81
81
|
// $.log(this.params);
|
82
82
|
// });
|
83
83
|
// });
|
84
|
-
//
|
84
|
+
//
|
85
85
|
// If you filled out the form with some values and submitted it, you would see something
|
86
86
|
// like this in your log:
|
87
|
-
//
|
87
|
+
//
|
88
88
|
// {
|
89
89
|
// 'obj': {
|
90
90
|
// 'first': 'value',
|
@@ -96,10 +96,10 @@
|
|
96
96
|
// }
|
97
97
|
// '$form': .. jQuery ..
|
98
98
|
// }
|
99
|
-
//
|
100
|
-
// It supports creating arrays with [] and other niceities. Check out the tests for
|
99
|
+
//
|
100
|
+
// It supports creating arrays with [] and other niceities. Check out the tests for
|
101
101
|
// full specs.
|
102
|
-
//
|
102
|
+
//
|
103
103
|
Sammy.NestedParams = function(app) {
|
104
104
|
|
105
105
|
$.extend(app, {
|
@@ -112,7 +112,7 @@
|
|
112
112
|
return params;
|
113
113
|
}
|
114
114
|
});
|
115
|
-
|
115
|
+
|
116
116
|
};
|
117
|
-
|
117
|
+
|
118
118
|
})(jQuery);
|