Skip to content

Instantly share code, notes, and snippets.

@JamieMason
Created December 28, 2010 17:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JamieMason/757462 to your computer and use it in GitHub Desktop.
Save JamieMason/757462 to your computer and use it in GitHub Desktop.
Note to self for the format of modules in JamieMVC
var simpleData = function (data)
{
var that = {};
// Decorators should always hold a simpleData instance, as you can nest instances
// we do checking here rather than in every decorator
if (typeof data.set === 'function')
{
return data;
}
// PRIVATE METHODS
// ################################################
// PUBLIC
// ################################################
that.set = function(candidate)
{
data = candidate;
// if changed etc here...
$(that).trigger('change');
}
that.get = function()
{
return data;
}
// INIT
// ################################################
return that;
}
var sortableData = function (data)
{
var that = {},
_super,
_data;
// PRIVATE METHODS
// ################################################
function _onChange(newData)
{
_data = newData + ' sorted';
return _data;
}
// PUBLIC
// ################################################
that.set = function(x)
{
// set our simpleData, if it changes our onChange will finish it off
_super.set(x);
}
that.get = function()
{
return _data;
}
// INIT
// ################################################
// always keep a simpleData instance
_super = simpleData(data);
// when the simpleData instance changes we process it and store it
_data = _onChange(_super.get());
// re-process our data whenever the simpleData instance changes
$(_super).bind('change', function()
{
_onChange(_super.get());
});
return that;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment