|
| 1 | +<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> |
| 2 | +<html xml:lang="en" xmlns="http://www.w3.org/1999/xhtml"> |
| 3 | +<head> |
| 4 | + <meta content="text/html;charset=UTF-8" http-equiv="Content-Type"/> |
| 5 | + <title>Test the CollectionBinder Sorting Capabilities</title> |
| 6 | + |
| 7 | + <!-- include source files here... --> |
| 8 | + <script type="text/javascript" src="../public/javascripts/underscore.js"></script> |
| 9 | + <script type="text/javascript" src="../public/javascripts/jquery.js"></script> |
| 10 | + <script type="text/javascript" src="../public/javascripts/backbone.js"></script> |
| 11 | + <script type="text/javascript" src="../Backbone.ModelBinder.js"></script> |
| 12 | + <script type="text/javascript" src="../Backbone.CollectionBinder.js"></script> |
| 13 | + |
| 14 | + <script> |
| 15 | + $().ready(function () { |
| 16 | + collection = new Backbone.Collection(); |
| 17 | + |
| 18 | + collection.comparator = function(model){ |
| 19 | + return model.get('order'); |
| 20 | + } |
| 21 | + |
| 22 | + collection.reset([ |
| 23 | + {id: 0, firstName: 'Adam', lastName: 'Zebra', order: 10}, |
| 24 | + {id: 1, firstName: 'Bob', phone: '1234567', order: 9} |
| 25 | + ]); |
| 26 | + |
| 27 | + collection.on('change:order', function(){ |
| 28 | + collection.sort(); |
| 29 | + }); |
| 30 | + |
| 31 | + // Display the collection content |
| 32 | + displayCollectionContent = function(){ |
| 33 | + var collectionVals = collection.reduce(function(memo, model){ |
| 34 | + return memo + ' (firstName=' + model.get('firstName') + ' order=' + model.get('order') + '), '; |
| 35 | + }, ''); |
| 36 | + $('#collectionContent').html(collectionVals); |
| 37 | + }; |
| 38 | + |
| 39 | + displayCollectionContent(); |
| 40 | + |
| 41 | + $('#refreshCollectionContent').click(displayCollectionContent); |
| 42 | + |
| 43 | + // Just create a simple view and attach the view's element to an existing DOM element |
| 44 | + view = new Backbone.View(); |
| 45 | + view.setElement($('#viewContent')); |
| 46 | + |
| 47 | + // Normally this will be in a separate html template file |
| 48 | + var rowHtml = '<tr><td data-name="firstName"></td><td data-name="lastName"></td><td data-name="phone"></td><td data-name="order"></td></tr>'; |
| 49 | + |
| 50 | + // The managerFactory helps to generate element managers - an el manager creates/removes elements when models are added to a collection |
| 51 | + // The 1st arg is the html template that is used to generate view elements for each model in the collection |
| 52 | + // The 2nd arg is the DOM element attribute that is used to bind between the Model attributes and DOM elements - it's optional |
| 53 | + var elManagerFactory = new Backbone.CollectionBinder.ElManagerFactory(rowHtml, "data-name"); |
| 54 | + |
| 55 | + collectionBinder = new Backbone.CollectionBinder(elManagerFactory, {autoSort: true}); |
| 56 | + |
| 57 | + // This is very similar to the ModelBinder.bind() function but the collectionBinder will also create nested element views |
| 58 | + collectionBinder.bind(collection, view.$('tbody')); |
| 59 | + |
| 60 | + |
| 61 | + |
| 62 | + // Demonstrating that the CollectionBinder has create/remove events you can listen to |
| 63 | + collectionBinder.on('elCreated', function(model, el){ |
| 64 | + console.log('The collectionBinder created an element ', model, el); |
| 65 | + }); |
| 66 | + |
| 67 | + collectionBinder.on('elRemoved', function(model, el){ |
| 68 | + console.log('The collectionBinder removed an element ', model, el); |
| 69 | + }); |
| 70 | + |
| 71 | + ////////////////////////////////////////////////////////////////////////////////////////////////// |
| 72 | + // The handlers below just help demonstrate that the rows in the table are bound to the collection |
| 73 | + modelCreateCount = 2; |
| 74 | + $('#createModel').on('click', function(){ |
| 75 | + collection.add({id: modelCreateCount, firstName: 'Jon ' + modelCreateCount, lastName: 'Doe ' + modelCreateCount, phone: 'xxx ', order: 10-collection.length}); |
| 76 | + modelCreateCount++; |
| 77 | + }); |
| 78 | + |
| 79 | + $('#removeModel').on('click', function(){ |
| 80 | + if(collection.length > 0){ |
| 81 | + collection.remove(collection.at(collection.length - 1)); |
| 82 | + } |
| 83 | + }); |
| 84 | + |
| 85 | + modelUpdateCount = 0; |
| 86 | + $('#updateModel').on('click', function(){ |
| 87 | + if(collection.length > 0){ |
| 88 | + collection.at(collection.length - 1).set({phone: 'xxx ' + modelUpdateCount}); |
| 89 | + modelUpdateCount++; |
| 90 | + } |
| 91 | + }); |
| 92 | + |
| 93 | + $('#resetCollection').on('click', function(){ |
| 94 | + collection.reset(); |
| 95 | + }); |
| 96 | + |
| 97 | + }); |
| 98 | + |
| 99 | + </script> |
| 100 | + |
| 101 | +</head> |
| 102 | +<body> |
| 103 | +<input type="button" id="refreshCollectionContent" value="Refresh"/> |
| 104 | +Collection content: <div id="collectionContent"></div> |
| 105 | +<br> |
| 106 | +Testing actions: |
| 107 | +<br> |
| 108 | +<input type="button" id="createModel" value="Create Model"/> |
| 109 | +<input type="button" id="removeModel" value="Remove Last Model"/> |
| 110 | +<input type="button" id="updateModel" value="Update Last Model's Phone"/> |
| 111 | + |
| 112 | +<input type="button" id="resetCollection" value="Reset Collection"/> |
| 113 | +<br> |
| 114 | +<br> |
| 115 | + |
| 116 | +View content: (sorted by the Order attribute)<hr> |
| 117 | +<div id="viewContent"> |
| 118 | + |
| 119 | + <table> |
| 120 | + <thead> |
| 121 | + <tr> |
| 122 | + <th>First Name</th> |
| 123 | + <th>Last Name</th> |
| 124 | + <th>Phone</th> |
| 125 | + <th>Order</th> |
| 126 | + </tr> |
| 127 | + </thead> |
| 128 | + |
| 129 | + <tbody> |
| 130 | + |
| 131 | + </tbody> |
| 132 | + </table> |
| 133 | +</div> |
| 134 | + |
| 135 | + |
| 136 | +</body> |
| 137 | +</html> |
0 commit comments