Skip to content

Commit

Permalink
Updated real sammy 0.6.1
Browse files Browse the repository at this point in the history
  • Loading branch information
quirkey committed Sep 25, 2010
1 parent 938b399 commit f6bb0c9
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 33 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -10,7 +10,7 @@ as a full interactive JavaScript application. couchapp's inherently have a
a bunch of really cool features - easy replication and synchronization,
instant access to store and fetch data from CouchDB, and a full JS API. [Sammy.js](http://code.quirkey.com/sammy) is a perfect fit for couchapps providing a simple programmable controller layer on top of CouchDB's data.

I highly recommend reading the section on couchapps in the [CouchDB Book](http://couchdb.apache.org/relax).
I highly recommend reading the section on couchapps in the [CouchDB Book](http://guide.couchdb.org/editions/1/en/standalone.html).

*soca* is a simple command line tool written in ruby for building and pushing
couchapps. It is similar to and heavily inspired by the canonical couchapp
Expand Down
21 changes: 17 additions & 4 deletions lib/soca/cli.rb
Expand Up @@ -126,10 +126,14 @@ def json(env = 'default')
say pusher(env).json
end

method_option '--compact', :type => :numeric, :default => 5,
:banner => 'run a compact operation every [n] pushes (default 5)'
desc 'autopush [ENV]', 'watches the current directory for changes, building and pushing to couchdb'
def autopush(env = 'default')
push = pusher(env)
files = {}
compact = options[:compact]
times = 0
loop do
changed = false
Dir.glob(push.app_dir + '**/**') do |file|
Expand All @@ -142,17 +146,26 @@ def autopush(env = 'default')
end

if changed
puts "Running push at #{Time.now}"
push.push!
say "Running push at #{Time.now}", :yellow
begin
push.push!
rescue => e
say "Error running push #{e}", :red
end

Dir.glob(push.app_dir + '**/**') do |file|
ctime = File.ctime(file).to_i
if ctime != files[file]
files[file] = ctime
end
end

puts "\nWaiting for a file change"
times += 1
if compact && times % compact == 0
say "pushed #{times} times, running a compact", :yellow
push.compact!
end
say "Waiting for a file change", :green
say "-------------------------"
end

sleep 1
Expand Down
97 changes: 69 additions & 28 deletions lib/soca/templates/js/vendor/sammy-0.6.1.js
@@ -1,7 +1,7 @@
// name: sammy
// version: 0.6.1pre
// version: 0.6.1

(function($) {
(function($, window) {

var Sammy,
PATH_REPLACER = "([^\/]+)",
Expand Down Expand Up @@ -70,7 +70,7 @@
}
};

Sammy.VERSION = '0.6.1pre';
Sammy.VERSION = '0.6.1';

// Add to the global logger pool. Takes a function that accepts an
// unknown number of arguments and should print them or send them somewhere
Expand Down Expand Up @@ -227,7 +227,7 @@
if (proxy.is_native === false && !non_native) {
Sammy.log('native hash change exists, using');
proxy.is_native = true;
clearInterval(Sammy.HashLocationProxy._interval);
window.clearInterval(Sammy.HashLocationProxy._interval);
}
app.trigger('location-changed');
});
Expand All @@ -242,7 +242,7 @@
$(window).unbind('hashchange.' + this.app.eventNamespace());
Sammy.HashLocationProxy._bindings--;
if (Sammy.HashLocationProxy._bindings <= 0) {
clearInterval(Sammy.HashLocationProxy._interval);
window.clearInterval(Sammy.HashLocationProxy._interval);
}
},

Expand All @@ -266,17 +266,17 @@
if (!Sammy.HashLocationProxy._interval) {
if (!every) { every = 10; }
var hashCheck = function() {
current_location = proxy.getLocation();
var current_location = proxy.getLocation();
if (!Sammy.HashLocationProxy._last_location ||
current_location != Sammy.HashLocationProxy._last_location) {
setTimeout(function() {
window.setTimeout(function() {
$(window).trigger('hashchange', [true]);
}, 13);
}
Sammy.HashLocationProxy._last_location = current_location;
};
hashCheck();
Sammy.HashLocationProxy._interval = setInterval(hashCheck, every);
Sammy.HashLocationProxy._interval = window.setInterval(hashCheck, every);
}
}
};
Expand Down Expand Up @@ -935,6 +935,7 @@
befores,
before,
callback_args,
path_params,
final_returned;

this.log('runRoute', [verb, path].join(' '));
Expand Down Expand Up @@ -1162,7 +1163,7 @@
},

_getFormVerb: function(form) {
var $form = $(form), verb;
var $form = $(form), verb, $_method;
$_method = $form.find('input[name="_method"]');
if ($_method.length > 0) { verb = $_method.val(); }
if (!verb) { verb = $form[0].getAttribute('method'); }
Expand Down Expand Up @@ -1274,6 +1275,9 @@
// called. This allows for the guarunteed order of execution while working
// with async operations.
//
// If then() is passed a string instead of a function, the string is looked
// up as a helper method on the event context.
//
// ### Example
//
// this.get('#/', function() {
Expand All @@ -1289,20 +1293,30 @@
// });
//
then: function(callback) {
if (_isFunction(callback)) {
var context = this;
if (this.waiting) {
this.callbacks.push(callback);
if (!_isFunction(callback)) {
// if a string is passed to then, assume we want to call
// a helper on the event context in its context
if (typeof callback === 'string' && callback in this.event_context) {
var helper = this.event_context[callback];
callback = function(content) {
return helper.apply(this.event_context, [content]);
};
} else {
this.wait();
setTimeout(function() {
var returned = callback.apply(context, [context.content, context.previous_content]);
if (returned !== false) {
context.next(returned);
}
}, 13);
return this;
}
}
var context = this;
if (this.waiting) {
this.callbacks.push(callback);
} else {
this.wait();
setTimeout(function() {
var returned = callback.apply(context, [context.content, context.previous_content]);
if (returned !== false) {
context.next(returned);
}
}, 13);
}
return this;
},

Expand Down Expand Up @@ -1369,7 +1383,7 @@
load: function(location, options, callback) {
var context = this;
return this.then(function() {
var should_cache, cached, is_json;
var should_cache, cached, is_json, location_array;
if (_isFunction(options)) {
callback = options;
options = {};
Expand All @@ -1381,6 +1395,7 @@
// its a path
is_json = (location.match(/\.json$/) || options.json);
should_cache = ((is_json && options.cache === true) || options.cache !== false);
context.next_engine = context.event_context.engineFor(location);
delete options.cache;
delete options.json;
if (options.engine) {
Expand Down Expand Up @@ -1503,7 +1518,8 @@
},

// loads a template, and then interpolates it for each item in the `data`
// array.
// array. If a callback is passed, it will call the callback with each
// item in the array _after_ interpolation
renderEach: function(location, name, data, callback) {
if (_isArray(name)) {
callback = data;
Expand All @@ -1515,11 +1531,19 @@
if (!data) {
data = _isArray(this.previous_content) ? this.previous_content : [];
}
return this.collect(data, function(i, value) {
var idata = {}, engine = this.next_engine || location;
name ? (idata[name] = value) : (idata = value);
return this.event_context.interpolate(content, idata, engine);
}, true);
if (callback) {
$.each(data, function(i, value) {
var idata = {}, engine = this.next_engine || location;
name ? (idata[name] = value) : (idata = value);
callback(value, rctx.event_context.interpolate(content, idata, engine));
});
} else {
return this.collect(data, function(i, value) {
var idata = {}, engine = this.next_engine || location;
name ? (idata[name] = value) : (idata = value);
return this.event_context.interpolate(content, idata, engine);
}, true);
}
});
},

Expand Down Expand Up @@ -1651,6 +1675,7 @@
if (engine && _isFunction(context[engine])) {
return context[engine];
}

if (context.app.template_engine) {
return this.engineFor(context.app.template_engine);
}
Expand Down Expand Up @@ -1680,6 +1705,22 @@
return new Sammy.RenderContext(this).render(location, data, callback);
},

// Create and return a `Sammy.RenderContext` calling `renderEach()` on it.
// Loads the template and interpolates the data for each item,
// however does not actual place it in the DOM.
//
// ### Example
//
// // mytemplate.mustache <div class="name">{{name}}</div>
// renderEach('mytemplate.mustache', [{name: 'quirkey'}, {name: 'endor'}])
// // sets the `content` to <div class="name">quirkey</div><div class="name">endor</div>
// renderEach('mytemplate.mustache', [{name: 'quirkey'}, {name: 'endor'}]).appendTo('ul');
// // appends the rendered content to $('ul')
//
renderEach: function(location, name, data, callback) {
return new Sammy.RenderContext(this).renderEach(location, name, data, callback);
},

// create a new `Sammy.RenderContext` calling `load()` with `location` and
// `options`. Called without interpolation or placement, this allows for
// preloading/caching the templates.
Expand Down Expand Up @@ -1765,4 +1806,4 @@
// An alias to Sammy
$.sammy = window.Sammy = Sammy;

})(jQuery);
})(jQuery, window);
1 change: 1 addition & 0 deletions soca.gemspec
Expand Up @@ -42,6 +42,7 @@ Gem::Specification.new do |s|
"lib/soca/templates/js/vendor/jquery-1.4.2.js",
"lib/soca/templates/js/vendor/jquery.couch-0.11.js",
"lib/soca/templates/js/vendor/sammy-0.6.1.js",
"lib/soca/templates/js/vendor/sammy.couch-0.1.0.js",
"lib/soca/templates/js/vendor/sha1.js",
"soca.gemspec",
"test/helper.rb",
Expand Down

0 comments on commit f6bb0c9

Please sign in to comment.