My backbone code, see below, is working without any issues, it gets the data and displays / renders that data using an underscore template. I have also add a trigger event to the backbone code, so that it gets JQuery to sort the template data into a sortable list.
However I have tried to update my sortable list with an update callback, which is meant to take the updated order of my list, convert it to JSON data, then using Backbone.save(); to save the data to my database.
My Backbone data ::
var AdminColModel = Backbone.Collection.extend({
url: '/Admin/GetMenuData'
});
var AdminEditMenu = Backbone.View.extend({
el: $(".page"),
render: function() {
var that = this;
var MyMenu = new AdminColModel();
MyMenu.fetch({
success: function(MyMenu) {
var menutemp = _.template( $('script.MenuTemplate').html(), {MyMenu: MyMenu.toJSON() } );
that.$el.html(menutemp);
that.trigger('MenuSortableList', that); <- fires my sort list!
}
});
} //End of render function
});
$(document).ready(function(){
var MyMenu = new AdminEditMenu();
MyMenu.on('MenuSortableList', function(event) {
$( "#AdminChangeMenuOrder" ).sortable({
axis: 'y',
placeholder: 'AdminMenuChangeableList',
helper: "clone",
forcePlaceholderSize: true,
forceHelperSize: true,
cursor: "move",
update: function() {
var sortableLinks = $(".AdminMenuChangeableList");
$(sortableLinks).sortable();
var linkOrderData = $(sortableLinks).sortable('serialize');
console.log(linkOrderData);
linkOrderData.save();
}
});
});
MyMenu.render();
});
Now this all works, apart from the code within the 'update' section call within the sortable call.
This is my underscore template,
<script class="MenuTemplate" type="text/template">
<section id="AdminChangeMenuOrder">
<% _.each(MyMenu, function(MyMenu) { %>
<div id="<%= MyMenu.id %>" class="AdminMenuChangeableList"><%= MyMenu.title %> @ <%= MyMenu.path %> Current Level : <%= MyMenu.level %></div>
<% }); %>
</section>
</script>
Now the template works, but I have been doing some research and it looks like a lot of people, use li
lists, is that what I should do with my template, is that easy to convert to a JSON object?
All help most welcome,
Thanks
Glenn