Marionette.StateView
Marionette.StateView is a simple extension of the default Marionette.View adding an additional bound model for maintaining view state.
Installation
Install this package via npm.
$ npm install marionette.stateview
Dependencies
All dependencies are listed in the package.json
as peerDependencies
, as this package wouldn't make any sense for any other environment.
Example
; const TestView = mnStateView;
Passing-in State
State passed into a new instance of a view will be applied to the instance's state model on construction, mixing-in any defaultState the view class defined:
; const TestView = mnStateView; const view = state: foo: 'foo-custom' other: 'other-value' ; console;// {// foo: 'foo-custom',// bar: 'bar-initial',// other: 'other-value',// }
Sharing state
State can be shared across multiple views by passing an existing state model in the view instance options. Any default state provided by the new view will be mixed into the state model that's passed:
; const TestViewA = mnStateView; const TestViewB = mnStateView; const viewA = ;const viewB = state: viewAstate ; console; // trueconsole;// {// foo: 'foo-initial',// bar: 'bar-initial',// }
Caveats
Marionette.StateView performs some custom actions in the View serializeData()
method, so if your view overwrites this function, be sure to invoke the default as well.
Contributing
Pull requests are always welcome! Please be sure to include any tests for new code & follow the current coding style as best you can.
You can run the test suite with the following command:
$ npm test
License
Any contributions made to this project are covered under the MIT License, found here.