Using Backbone.js
, Marionette.js
and gridstack.js
to render a grid view of widgets
Blue
are the Backbone Marionette Widget Gridview classes.Green
are the custom classes created by you to customize the widget gridview.
Here is the definition of the classes shown in the diagram above:
- The
Backbone.Widget
is aBackbone.Model
that has different properties about the widget in the grid view. The most important properties are the position (x and y inside the grid), width and height and lastly the type of view associated with this widget. - The
Backbone.WidgetList
is aBackbone.Collection
that holds multipleBackbone.Widget
s. It is possible to extend theBackbone.WidgetList
to make your custom collection. In the localstorage example, a custom collection is created to use with backbone.localstorage Marionette.WidgetView
is aMarionette.ItemView
and it's there so you can extend it and create your own custom views with custom templates.Marionette.WidgetGridView
is aMarionette.LayoutView
. ABackbone.WidgetList
should be passed when creating this view. TheMarionette.WidgetGridView
will create aMarionette.Region
for every model inside its collection. Whenrender()
is called on this view, it will proceed to create and show a view for every model based on the model'sviewType
property.
Interactions between the classes :
- When you modify
Marionette.WidgetGridView
's collection y adding or removing a widget, it will automatically update the grid view by adding or removingMarionette.WidgetView
- Your custom
Marionette.WidgetView
s should have a way to trigger aremove:widget
event, like by clicking on a close button inside the view (see the localstorage example on how to do that). The event will be catched by theMarionette.WidgetGridView
and it will then proceed to remove the widget view from the grid. - When there is a modification to the grid, be it the position or size of widgets that changed, the models inside the collection of
Marionette.WidgetGridView
will be updated and the collection will be saved using theautoSave
options's save callback.
- Clone the repo.
- Naviguate to the folder containing the package.json file
- Install the dependencies with (you need to have node.js installed first) : npm install
- jQuery (>= 1.11.0)
- jQuery UI (>= 1.11.0). Minimum required components: Core, Widget, Mouse, Draggable, Resizable
- underscore.js (>= 1.7.0)
- Backbone.js (>= 1.1.2)
- Marionette.js (>= 2.4.1)
- Gridstack.js (>= 0.2.3-dev)
- Move and resize widgets based on the functionality present in
gridstack.js
. - Add and remove widgets to the gridview.
- Save a collection of widgets using a custom defined method to save (see
saveCollectionOnLocalStorage
function in example/localstorage/index.html).
- Create some
Backbone.Widget
s - Create a
Backbone.WidgetList
with the created widgets - Create a
Marionette.WidgetGridView
and pass as the parameters the collection you created and some options - Reference the html with the gridview's
.$el
and thenrender()
- Have fun with the widgets
Look at the basic example for the simplest way to use this library !
Options for WidgetGridView :
customViews
: an array of definitions where keys represents the names of the custom views and values the references to the custom views that you extended from the base classMarionette.WidgetView
.autoPos
: a boolean that is passed to gridstack's add_widget function. Iftrue
, gridstack will ignore the specified x and y grid values and will add the widget to the next available position. Iffalse
, gridstack will try to add the widget at the specified x and y grid values and add it to the next position available if the space is already occupied. (default set totrue
)autoSave
: Javascript Object containing a callback that will be used to save the widgets when their attributes have changed. The callback can implement a save to a database, save to localstorage or anything you want. You can also specify some options in theautoSave
object, which will be passed to the callback when it's time to save. Options can be a Javascript Object containing key and values or a function that will return some options.logHelper
: Javascript Object containing a callback to print/output messages, a context for that callback and some predefined messages to display
Options for gridstack.js : Look at the gridstack docs for the available options
It is possible to change the behavior of the grid view (and gridstack) by changing its options (using Marionette.WidgetGridView
's setAutoPos()
and setGridstackOptions()
). The only thing left to do is to call render()
again on the Marionette.WidgetGridView
so the new options are used.
Event Name | Triggered When | Associated Callback | Callback functionality |
---|---|---|---|
add |
a Backbone.Widget is added to the Backbone.WidgetList that was passed when creating the Marionette.WidgetGridView |
onCollectionAdd |
add the Backbone.Widget 's associated Marionette.WidgetView to the gridview |
remove |
a Backbone.Widget is removed from the Backbone.WidgetList . It is also triggered when you make a Backbone.Widget 's associated Marionette.WidgetView trigger a remove:widget event. |
onCollectionRemove |
remove the Backbone.Widget 's associated Marionette.WidgetView from the gridview |
reset |
a reset() is called on the Backbone.WidgetList |
onCollectionReset |
remove all the widgets from the gridview, re-initialize Gridstack and update the gridview if there are new Backbone.Widget in the collection |
change |
a Backbone.Widget 's attribute changes |
onModelChange |
save the entire collection using the save callback that was passed when creating the Marionette.WidgetGridView |
The main customization is to extend the Marionette.WidgetView
to make your own custom widget views !
You can see an example of doing so by taking a look at the localstorage example that comes with this library at example/localstorage/index.html
When a Marionette.WidgetGridView
is render()
ed, it will initialize gridstack with the current gsOptions
and then proceed to add a custom view to the grid view for every model inside the current collection.
When a Marionette.WidgetView
is rendered, it will only render the root element. That means that when you define the template of your custom views, you should always regroup your view's html into a single element at the top level. . In the localstorage example, the content of the views are contained into a single <div></div>
html tag.
- Implemented the basic functionalities of adding and removing widgets, saving the entire collection of widgets.
- Customizable options to log help/errors messages
- Support of AMD