Created
February 24, 2010 15:23
-
-
Save bsingr/313513 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Works just like <tt>jQuery.fn.bind()</tt> with a couple noteable differences. | |
// | |
// * It binds all events to the application element | |
// * All events are bound within the <tt>eventNamespace()</tt> | |
// * Events are not actually bound until the application is started with <tt>run()</tt> | |
// * callbacks are evaluated within the context of a Sammy.EventContext | |
// | |
// See http://code.quirkey.com/sammy/docs/events.html for more info. | |
// | |
bind: function(name, data, callback) { | |
var app = this; | |
// build the callback | |
// if the arity is 2, callback is the second argument | |
if (typeof callback == 'undefined') callback = data; | |
var listener_callback = function() { | |
// pull off the context from the arguments to the callback | |
var e, context, data; | |
e = arguments[0]; | |
data = arguments[1]; | |
/* | |
dpree: here we forget the third argument from sammy.storage!, e.g.: | |
bind('set-kvo.foo', fn(event,myKey,myValue){ | |
myKey == 'foo'; // true <== ok, altough it's somehow senseless, as it is already included in the event-name?! | |
myValue; // undefined <== the problem! | |
}); | |
as a temporary solution i think it would be appropriate to change the trigger-mechanism | |
in sammy.storage like shown below.. | |
=> however, we should become aware of more than just three args, like: | |
$(..).trigger('myevent', [1,2,3,4,..,n]); | |
*/ | |
if (data && data['context']) { | |
context = data['context'] | |
delete data['context']; | |
} else { | |
context = new app.context_prototype(app, 'bind', e.type, data); | |
} | |
e.cleaned_type = e.type.replace(app.eventNamespace(), ''); | |
callback.apply(context, [e, data]); | |
}; | |
// it could be that the app element doesnt exist yet | |
// so attach to the listeners array and then run() | |
// will actually bind the event. | |
if (!this.listeners[name]) this.listeners[name] = []; | |
this.listeners[name].push(listener_callback); | |
if (this.isRunning()) { | |
// if the app is running | |
// *actually* bind the event to the app element | |
this._listen(name, listener_callback); | |
} | |
return this; | |
}, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Sets the value of <tt>key<tt> with <tt>value</tt>. If <tt>value<tt> is an | |
// object, it is turned to and stored as a string with <tt>JSON.stringify</tt>. | |
// It also tries to conform to the KVO pattern triggering jQuery events on the | |
// element that the store is bound to. | |
// | |
// === Example | |
// | |
// var store = new Sammy.Store({name: 'kvo'}); | |
// $('body').bind('set-kvo.foo', function() { | |
// alert('foo changed!') | |
// }); | |
// store.set('foo', 'bar'); // alerted: foo changed! | |
// | |
set: function(key, value) { | |
var string_value = (typeof value == 'string') ? value : JSON.stringify(value); | |
key = key.toString(); | |
this.storage.set(key, string_value); | |
if (key != this.meta_key) { | |
this._addKey(key); | |
this.$element.trigger('set-' + this.name + '.' + key, [key, value]); | |
/* | |
dpree's temporary solutions: | |
maybe: | |
this.$element.trigger('set-' + this.name + '.' + key, {key: key, value: value}); | |
or just simply: | |
this.$element.trigger('set-' + this.name + '.' + key, value); | |
*/ | |
}; | |
return string_value; | |
}, |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment