Skip to content

Commit

Permalink
Added support for Action "hang back"
Browse files Browse the repository at this point in the history
  • Loading branch information
jdubray committed Apr 2, 2016
1 parent 9c05ea6 commit bf86bd6
Show file tree
Hide file tree
Showing 9 changed files with 425 additions and 78 deletions.
5 changes: 5 additions & 0 deletions README.md
Expand Up @@ -37,3 +37,8 @@ A session manager can be plugged into SAFE to manage session rehydration/hydrati
### Allowed Actions

Currently SAFE can be used enforce global action authorization (not yet session based)

### Action Hang Back

SAFE supports a mode where the client can call multiple allowed actions in a given step. The first one to present its values "wins" and prevents any other
action to present its values to the model
58 changes: 50 additions & 8 deletions actions.js
@@ -1,32 +1,74 @@
//
///////////////////////////////////////////////////////////////////////////////
// This is free and unencumbered software released into the public domain.
//
// Anyone is free to copy, modify, publish, use, compile, sell, or
// distribute this software, either in source code form or as a compiled
// binary, for any purpose, commercial or non-commercial, and by any
// means.
//
// In jurisdictions that recognize copyright laws, the author or authors
// of this software dedicate any and all copyright interest in the
// software to the public domain. We make this dedication for the benefit
// of the public at large and to the detriment of our heirs and
// successors. We intend this dedication to be an overt act of
// relinquishment in perpetuity of all present and future rights to this
// software under copyright law.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
// OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
//
// For more information, please refer to <http://unlicense.org/>

'use strict' ;

var actions = {} ;

actions.init = (present) => {
actions.present = present ;
} ;

actions.present = (data) => {
return false ;
} ;

actions.edit = function(data,next) {
actions.edit = (data,next) => {
data.lastEdited = {title: data.title, description: data.description, id: data.id } ;
actions.present(data,next) ;
return false ;
} ;

actions.save = function(data,next) {
data.item = {title: data.title, description: data.description, id: data.id || null} ;
actions.present(data,next) ;
actions.save = (data,next) => {
data.item = {title: data.title, description: data.description, id: data.id || ''} ;
if (data.item.id !== '') {
// simulate a slow save after
// editing an item
setTimeout(function() {
actions.present(data,next) ;
}, 9000);
}
else {
// proceed as normal when created a new item
actions.present(data,next) ;
}
return false ;
} ;

actions.delete = function(data,next) {
data = {deletedItemId: data.id} ;
actions.delete = (data,next) => {
data.deletedItemId = data.id ;
actions.present(data,next) ;
return false ;
} ;

actions.cancel = function(data,next) {
actions.cancel = (data,next) => {
actions.present(data,next) ;
return false ;
} ;

actions.intents = { edit: 'edit', save: 'save', delete: 'delete', cancel: 'cancel' } ;

module.exports = actions ;
14 changes: 14 additions & 0 deletions blog.html
Expand Up @@ -87,6 +87,18 @@ <h1 class="blog-title">The SAM Pattern Blog</h1>
// This sample was inspired from @rajaraodv
// https://medium.com/@rajaraodv/a-guide-for-building-a-react-redux-crud-app-7fe0b8943d0f#.e7501tdde

function dispatch(data) {
// client side
//model.present(data) ;

// server side
$.post( "http://localhost:5425/app/v1/dispatch", data)
.done(function( representation ) {
$( "#representation" ).html( representation );
}
);
}

function present(data) {
// client side
//model.present(data) ;
Expand All @@ -103,6 +115,8 @@ <h1 class="blog-title">The SAM Pattern Blog</h1>
// client side
//view.display(view.init(model)) ;

actions.do = dispatch

// server side
$.get( "http://localhost:5425/app/v1/init", function( data ) {
$( "#representation" ).html( data );
Expand Down
57 changes: 45 additions & 12 deletions blog.js
@@ -1,3 +1,31 @@
///////////////////////////////////////////////////////////////////////////////
// This is free and unencumbered software released into the public domain.
//
// Anyone is free to copy, modify, publish, use, compile, sell, or
// distribute this software, either in source code form or as a compiled
// binary, for any purpose, commercial or non-commercial, and by any
// means.
//
// In jurisdictions that recognize copyright laws, the author or authors
// of this software dedicate any and all copyright interest in the
// software to the public domain. We make this dedication for the benefit
// of the public at large and to the detriment of our heirs and
// successors. We intend this dedication to be an overt act of
// relinquishment in perpetuity of all present and future rights to this
// software under copyright law.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
// OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
//
// For more information, please refer to <http://unlicense.org/>



////////////////////////////////////////////////////////////////////////////////
// Model
//
Expand Down Expand Up @@ -159,28 +187,33 @@ state.render = function(model,next) {
// Actions
//


var actions = {} ;

actions.edit = function(data) {
data.lastEdited = {title: data.title, description: data.description, id: data.id } ;
present(data) ;
actions.edit = (data) => {
// data.lastEdited = {title: data.title, description: data.description, id: data.id } ;
data.__action = 'edit' ;
actions.do(data) ;
return false ;
} ;

actions.save = function(data) {
data.item = {title: data.title, description: data.description, id: data.id || null} ;
present(data) ;
actions.save = (data) => {
// data.item = {title: data.title, description: data.description, id: data.id || null} ;
data.__action = 'save' ;
actions.do(data) ;
return false ;
} ;

actions.delete = function(data) {
data = {deletedItemId: data.id} ;
present(data) ;
actions.delete = (data) => {
data.__action = 'delete' ;
actions.do(data) ;
return false ;
} ;

actions.cancel = function(data) {
present(data) ;
actions.cancel = (data) => {
data = data || {} ;
data.__action = 'cancel'
actions.do(data) ;
return false ;
} ;


34 changes: 31 additions & 3 deletions model.js
@@ -1,3 +1,29 @@
///////////////////////////////////////////////////////////////////////////////
// This is free and unencumbered software released into the public domain.
//
// Anyone is free to copy, modify, publish, use, compile, sell, or
// distribute this software, either in source code form or as a compiled
// binary, for any purpose, commercial or non-commercial, and by any
// means.
//
// In jurisdictions that recognize copyright laws, the author or authors
// of this software dedicate any and all copyright interest in the
// software to the public domain. We make this dedication for the benefit
// of the public at large and to the detriment of our heirs and
// successors. We intend this dedication to be an overt act of
// relinquishment in perpetuity of all present and future rights to this
// software under copyright law.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
// OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
//
// For more information, please refer to <http://unlicense.org/>


var model = {
posts: [
Expand All @@ -21,7 +47,7 @@ model.init = function(render) {

model.present = function(data,next) {
data = data || {} ;

console.log(data) ;
if (data.deletedItemId !== undefined) {
var d = -1 ;
model.posts.forEach(function(el,index) {
Expand All @@ -45,9 +71,9 @@ model.present = function(data,next) {
if (data.item !== undefined) {

if (data.item.id !== "") {
// has been edited
// item has been edited
model.posts.forEach(function(el,index) {
if (el.id !== undefined) {
if (el.id !== null) {
if (el.id == data.item.id) {
model.posts[index] = data.item ;
}
Expand All @@ -61,6 +87,8 @@ model.present = function(data,next) {
}
}

console.log(model) ;

model.render(model,next) ;
} ;

Expand Down

0 comments on commit bf86bd6

Please sign in to comment.