Created
August 6, 2015 10:10
-
-
Save dervalp/26841b25c4bdc56abb01 to your computer and use it in GitHub Desktop.
PlayMakerSample
This file contains hidden or 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
var TodoPlayMaker = new PlayMaker({ | |
name:'TodoPlayMaker', | |
events: [ 'REMOVE_TODO', 'ADD_TODO' ] | |
}); | |
var UserPlayMaker = new PlayMaker({ | |
name: 'UserPlayMaker', | |
events: [ 'LOGOUT' ] | |
}); | |
var _todos = [{ id: 1, text:'Create Doc'}]; | |
//In a Service you can only receive actions | |
var TodoService = new TodoService({ | |
name: 'TodoStore', | |
receive: function ( event, done ) { | |
if( event.id === 'REMOVE_TODO' ) { | |
this.remove(event.data); | |
} | |
if( event.id === 'REMOVE_TODO' && this.WaitUntil('ADD_TODO')) { | |
//something else | |
} | |
if( event.id === 'ADD_TODO' ) { | |
this.add(event.data).then(function(){ | |
done(event, payLoad); | |
}); | |
} | |
}, | |
getAll: function ( ) { | |
setTimeOut(function(){ | |
}, 1000); | |
}, | |
remove: function ( index ) { | |
delete _todos[index]; | |
}, | |
add: function ( todo ) { | |
_todos.push(todo); | |
} | |
}); | |
var UserService = new UserService({ | |
name: 'UserStore', | |
receive: function(event) { | |
if(event.id === 'LOGOUT') { | |
this.logout(); | |
} | |
}, | |
logout: function () { | |
//do the ajax call | |
} | |
}); | |
//In a PageCode you can only 'play' actions. | |
Sitecore.Speak.pageCode({ | |
playMaker: ['TodoPlayMaker', 'UserPlayMaker'], | |
initialize: function() { | |
var self = this; | |
this.playMakers.TodoPlayMaker.playAll('TODO_ALL', 'TODO_FILTER').then(function(payLoad1, payLoad2){ | |
self.component.Data = payLoad1; | |
}); | |
this.playMakers.TodoPlayMaker.play( 'TODO_ALL' ).then(function(payLoad){ | |
self.ListControl.Data = payLoad; | |
}); | |
}, | |
removeTodo: function() { | |
this.playMakers.TodoPlayMaker.play( 'REMOVE_TODO', this.DropDown01.SelectedId ); | |
}, | |
logout: function () { | |
var self = this; | |
this.playMakers.UserPlayMaker.play( 'LOGOUT' ); | |
//.then(function{ | |
// self.hideSomePanel(); | |
//}); | |
}, | |
hideSomePanel: function () { | |
this.hideSomePanel(); | |
} | |
}); | |
//You can share a PlayMaker | |
Sitecore.Speak.pageCode({ | |
name: 'subPageCode', | |
playMaker: ['TodoPlayMaker'], | |
removeFromAnotherButton: function() { | |
this.playMakers.TodoPlayMaker.play( 'REMOVE_TODO', this.Input01.SelectedId ); | |
}; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment