datatables and backbone.js
datatables and backbone.js
Srilakshmi
Posts: 35Questions: 6Answers: 0
Hi,
can someone tell me how to use datatables with backbone.js
Regards,
Srilakshmi
can someone tell me how to use datatables with backbone.js
Regards,
Srilakshmi
This discussion has been closed.
Replies
There is a significant overlap in the functionality provided by the two libraries, and they would conflict without directly integration between he two. That is likely to be quite an undertaking, with several days of work. I'm not aware of anyone having done it yet - in part, because I don't fully understand what the benefit would be from such an integration. What would you get from using the two together?
Allan
Peter
Allan
hi,
Ive done some of work on this front using bootstrap editable. You can edit the source to use the backbone model instead of the default datatable model for data. This paired with a change event allows you to implement things like two way binding. You can bind at the cell, row, or table level. Depends on if your setting up a view for each cell or row.
I edit the _fnGetCellData to
I then edit the _fnCreateTr
You can also bind on the row level using fnCreatedRow and nRow/aData.
Using DataTables alone on a simple page is no problem. When the application gets more complex, using pure jQuery plugins leads to maintenance problems and is not an option, especially in enterprise SPAs which consist of many forms, navigation and detail views. Using patterns like MVC help to organize the application. Backbone and DataTables will have different purpose in such application:
Backbone separates the data from the presentation layer by letting Backbone.Model manage them. Getting and setting values, sorting, filtering, paging, AJAX - they are performed by the model. You can have different views - tables, lists etc. Why should every UI control be able to sort the data, for example? The Backbone.View renders the data and handles events connected to the GUI. Instead of creating the HTML markup from the scratch, the view can make use of other UI controls - hence the efforts to plug DataTables into a Backbone view. However, here comes the catch, because DataTables performs the data handling too.
I use DataTables 1.9.4 with Backbone 1.1.0 and it works reasonably well. If you accept that you will use only the GUI part of the DataTables and ignore its data processing. DataTables lets the data handling be overridden in custom code. I needed just one modification of the original code - prevent cloning of the data when a row is added. I added a boolean
bCloneData
flag which I set tofalse
when integrating with Backbone:.For the beginning, you tell DataTables how to get the data for rendering by
mData
and/ormRender
for more complicated cells.```
aaData: this.collection.models,
aoColumns: $.map(['Type', 'Name', 'Modified'], function (name) {
return {
sName: name, sTitle: name,
mData: function (source, type, value) {
if (type === 'set') {
source.set(name, value);
} else {
return source.get(name);
}
},
...
};
},
...
...
Then you continue overriding according to your application needs. In my case, the Backbone.Collection was supported by the server-side paging, sorting and filtering, so I ended up with overriding
fnServerData
and fetching the collection according to the DataTables state extracted fromoSettings
. Actually, the server-side processing (bServerSide = true
) effectively turns off the internal data processing and lets it on the Backbone. I'd recommend it for the Backbone + DataTables integration, although you may not load the data by AJAX.Refreshing the data can be done by calling
fnDraw
when the whole page changes (the collection gets synced) or by binding models to artificially created Backbone.View instances infnRowCallback
(DataTables creates the markup; not Backbone).Hi,
Thanks for the feedback. The clone of the data object has been removed in 1.10 in order to lay the ground work for things like this. There a number of other improvements which might also help such as being able to supply object instances as a data source for the row.
Allan