Skip to content

Commit

Permalink
several improvements to te facade security layer, adding some context…
Browse files Browse the repository at this point in the history
… to the README so that devs can see how its used
  • Loading branch information
addyosmani committed Nov 14, 2011
1 parent 8f05914 commit ac8bffb
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 33 deletions.
10 changes: 5 additions & 5 deletions README.md
@@ -1,10 +1,10 @@
##Backbone-mediator
##Backbone-aura

A Todo application using:

<ul>
<li>The mediator pattern for centralized Pub/Sub</li>
<li>(Initial) facade integration</li>
<li>Facade pattern for security/permissions</li>
<li>RequireJS</li>
<li>AMD modules</li>
<li>Backbone.js & Underscore.js</li>
Expand All @@ -23,9 +23,9 @@ Based on portions by Ryan Rauh [3]
<ul>
<li>[1] http://backbonetutorials.com/organizing-backbone-using-modules</li>
<li>[2] http://addyosmani.com/writing-modular-js</li>
<li>[3] https://github.com/rauhryan/Backbone_RequireJS</li>
<li>[3] https://github.com/addyosmani/Backbone_RequireJS or https://github.com/rauhryan/Backbone_RequireJS</li>
</ul>

Todos:
For demonstration:

Better express the concept of the facade and sample usage.
It's useful to see how easy it is for the application logic to be decoupled now that it's based on centralized pub/sub. Also try using the permissions manager (permissions.js). By changing say, permissions -> renderDone -> todoCounter to be false, you can completely disable that component from displaying counts (because it isn't allowed to subscribe to that event). Nifty, eh?
11 changes: 7 additions & 4 deletions app/scripts/backbone/todo/facade.js
Expand Up @@ -4,10 +4,13 @@ define([ "../todo/mediator" , "../todo/permissions" ], function (mediator, permi

var facade = facade || {};

facade.subscribe = function(channel, subscription){
//optional: handle persmissions
//if(permissions.validate)
mediator.subscribe( channel, subscription );
facade.subscribe = function(subscriber, channel, callback){
// optional: handle persmissions
// the conditional permissions check can be removed
// to just use the mediator directly.
if(permissions.validate(subscriber, channel)){
mediator.subscribe( channel, callback );
}
}

facade.publish = function(channel){
Expand Down
37 changes: 17 additions & 20 deletions app/scripts/backbone/todo/modules.js
@@ -1,12 +1,12 @@
define(["../todo/facade"], function (facade) {


//Subscriber modules for Todo view

//Subscriber modules for Todo view with named
// subscribers

// Desc: Update view with latest todo content
// Subscribes to: newContentAvailable
facade.subscribe('newContentAvailable', function (context) {

facade.subscribe('contentUpdater', 'newContentAvailable', function (context) {
var content = context.model.get('content');
context.$('.todo-content').text(content);
context.input = context.$('.todo-input');
Expand All @@ -15,19 +15,9 @@ define(["../todo/facade"], function (facade) {
});



// Desc: update editing UI on switching mode to editing content
// Subscribes to: beginContentEditing
facade.subscribe('beginContentEditing', function (context) {
$(context.el).addClass("editing");
context.input.focus();
});



// Desc: Save models when a user has finishes editing
// Subscribes to: endContentEditing
facade.subscribe('endContentEditing', function (context) {
facade.subscribe('todoSaver','endContentEditing', function (context) {
try {
context.model.save({
content: context.input.val()
Expand All @@ -38,11 +28,18 @@ define(["../todo/facade"], function (facade) {
}
});

// Desc: update editing UI on switching mode to editing content
// Subscribes to: beginContentEditing
facade.subscribe('editFocus','beginContentEditing', function (context) {
$(context.el).addClass("editing");
context.input.focus();
});



// Desc: Delete a todo when the user no longer needs it
// Subscribes to: destroyContent
facade.subscribe('destroyContent', function (context) {
facade.subscribe('todoRemover','destroyContent', function (context) {
try {
context.model.clear();
} catch (e) {
Expand All @@ -54,7 +51,7 @@ define(["../todo/facade"], function (facade) {

// Desc: When a user is adding a new entry, display a tooltip
// Subscribes to: addingNewTodo
facade.subscribe('addingNewTodo', function (context, todo) {
facade.subscribe('todoTooltip','addingNewTodo', function (context, todo) {
var tooltip = context.$(".ui-tooltip-top");
var val = context.input.val();
tooltip.fadeOut();
Expand All @@ -70,7 +67,7 @@ define(["../todo/facade"], function (facade) {

// Desc: Create a new todo entry
// Subscribes to: createWhenEntered
facade.subscribe('createWhenEntered', function (context, e, todos) {
facade.subscribe('keyboardManager','createWhenEntered', function (context, e, todos) {
if (e.keyCode != 13) return;
todos.create(context.newAttributes());
context.input.val('');
Expand All @@ -80,7 +77,7 @@ define(["../todo/facade"], function (facade) {

// Desc: A Todo and remaining entry counter
// Subscribes to: renderDone
facade.subscribe('renderDone', function (context, Todos) {
facade.subscribe('todoCounter','renderDone', function (context, Todos) {
var done = Todos.done().length;
context.$('#todo-stats').html(context.statsTemplate({
total: Todos.length,
Expand All @@ -92,7 +89,7 @@ define(["../todo/facade"], function (facade) {

// Desc: Clear all completed todos when clearContent is dispatched
// Subscribes to: clearContent
facade.subscribe('clearContent', function (Todos) {
facade.subscribe('garbageCollector','clearContent', function (Todos) {
_.each(Todos.done(), function (todo) {
todo.clear();
});
Expand Down
51 changes: 47 additions & 4 deletions app/scripts/backbone/todo/permissions.js
Expand Up @@ -3,10 +3,53 @@ define([], function () {
// Permissions

// A permissions structure can check
// against subscriptions and publications
// from methods before allowing them to
// clear
// against subscriptions prior to allowing
// them to clear. This enforces a light
// security layer for your application.

// return permissions;
var permissions = {

newContentAvailable: {
contentUpdater:true
},

endContentEditing:{
todoSaver:true
},

beginContentEditing:{
editFocus:true
},

addingNewTodo:{
todoTooltip:true
},

clearContent:{
garbageCollector:true
},

renderDone:{
todoCounter:false
},

destroyContent:{
todoRemover:true
},

createWhenEntered:{
keyboardManager:true
}

};

permissions.validate = function(subscriber, channel){
var test = permissions[channel][subscriber];
console.log(test);
return test===undefined? false: test;
};


return permissions;

});

0 comments on commit ac8bffb

Please sign in to comment.