]> source.dussan.org Git - archiva.git/commitdiff
doc on knockout binding
authorOlivier Lamy <olamy@apache.org>
Mon, 23 Jan 2012 20:45:12 +0000 (20:45 +0000)
committerOlivier Lamy <olamy@apache.org>
Mon, 23 Jan 2012 20:45:12 +0000 (20:45 +0000)
git-svn-id: https://svn.apache.org/repos/asf/archiva/trunk@1234980 13f79535-47bb-0310-9956-ffa450edef68

archiva-modules/archiva-web/archiva-webapp-js/src/main/webapp/index.html
archiva-modules/archiva-web/archiva-webapp-js/src/main/webapp/js/archiva/i18nload.js
archiva-modules/archiva-web/archiva-webapp-js/src/site/apt/knockout-binding.apt
archiva-modules/archiva-web/archiva-webapp-js/src/site/xdoc/index.xml

index 6b11e4d4f8d7950af1f74db6a586234ec3b6ac1e..588fb81c0e08454e17b1e52a9745ce31ccc7bd4f 100644 (file)
         baseUrl: "js/"
       });
     // CacheBust is for dev purpose use false in prod env !
-    $LAB.setGlobalDefaults({AlwaysPreserveOrder:true,BasePath:"js/",explicit_preloading:false,CacheBust:true});
+    var options = {
+        AlwaysPreserveOrder:true,
+        BasePath:"js/",
+        explicit_preloading:false,
+        CacheBust:true
+    };
+    $LAB.setGlobalDefaults(options);
     $LAB
        .script("jquery.tmpl.js").wait()
        .script("archiva/utils.js").wait()
        .script("bootstrap-tabs.js")
        .script("bootstrap-modal.js")
        .script("bootstrap-alerts.js")
-       .script("bootstrap-dropdown.js").wait()
-       .script("bootstrap-twipsy.js").wait()
+       .script("bootstrap-dropdown.js")
+       .script("bootstrap-twipsy.js")
        .script("bootstrap-popover.js")
-       .script("knockout.simpleGrid.js").wait()
-       .script("knockout.mapping-latest.debug.js").wait()
+       .script("knockout.simpleGrid.js")
+       .script("knockout.mapping-latest.debug.js")
        .script("redback/user.js").wait()
        .script("redback/users.js").wait()
        .script("redback/redback.js").wait()
@@ -74,9 +80,7 @@
        .script("redback/permission.js").wait()
        .script("redback/resource.js").wait()
        .script("redback/roles.js").wait()
-       .script("archiva/main.js").wait(function(){
-        //startArchivaApplication();
-       });
+       .script("archiva/main.js");
 
 </script>
 
index 974a109210f4f6a2d9aab056ebdd6a76750bc3d3..227aa22153a49436b22587cde767a75adb6fc005 100644 (file)
@@ -29,12 +29,12 @@ $(function() {
   // -- archiva
   // load default
   //loadAndParseFile("restServices/archivaServices/commonServices/getAllI18nResources", {cache:false, mode: 'map',encoding:'utf-8'});
-  //if (browserLang!='en'){
+
     var options = {
       cache:false,
       mode: 'map',
       encoding:'utf-8'
     };
     loadAndParseFile("restServices/archivaServices/commonServices/getAllI18nResources?locale="+browserLang,options );
-  //}
+
 });
\ No newline at end of file
index 110d726b78c93a242c82ab0e3a893f1e3e64caed..54cab62cca33039f2889a7b238e3384801314b9c 100644 (file)
 ~~ NOTE: For help with the syntax of this file, see:
 ~~ http://maven.apache.org/guides/mini/guide-apt-format.html
 
-Knockout binding
\ No newline at end of file
+Knockout binding
+
+  Explanation on the managed repositories list/edit/add screen.
+
+%{toc}
+
+* Javascript Beans
+
+  First you must map the json response on a Javascript bean (a bit borying task :-) )
+
++-------------------
+Java class with fields
+
+public class ManagedRepository
+  //private String id;
+
+  //private String name;
+  ....
+
+Javascript
+ManagedRepository=function(id,name,.....){
+
+  this.id=ko.observable(id);
+
+  this.name=ko.observable(name);
+
+mapping function (to map the json result to your javascript beans)
+
+  mapManagedRepositories=function(data){
+    var mappedManagedRepositories = $.map(data.managedRepository, function(item) {
+      return mapManagedRepository(item);
+    });
+    return mappedManagedRepositories;
+  }
+  mapManagedRepository=function(data){
+    if (data==null){
+      return null;
+    }
+    return new ManagedRepository(data.id,data.name,data.layout,data.indexDirectory,data.location,data.snapshots,data.releases,
+                                 data.blockRedeployments,data.cronExpression,
+                                 data.scanned,data.daysOlder,data.retentionCount,data.deleteReleasedSnapshots,data.stageRepoNeeded);
+  }
+
++-------------------
+
+   <<<NOTE to have access to field values you must now managedRepository.name() >>>
+
+* View Model
+
+** First you must insert your template in the #main-content div
+
++---------------
+// it's a jquery template as we do some i18n transformations
+$("#main-content").html($("#repositoriesMain").tmpl());
++---------------
+
+** You can now create your view model.
+
++---------------
+ManagedRepositoriesViewModel=function(){
+  //field which will receive values
+  this.managedRepositories=ko.observableArray([]);
+  // method which will edit an entry: an other view model is created
+  editManagedRepository=function(managedRepository){
+    var viewModel = new ManagedRepositoryViewModel(managedRepository,true,self);
+    ...
+    ko.applyBindings(viewModel,$("#main-content #managed-repository-edit").get(0));
+    ..
+  }
+  // method which will delete an entry
+  removeManagedRepository=function(managedRepository){
+    ......
+  }
+}
++---------------
+
+** Grid binding
+
+  The ManagedRepositoriesViewModel is used as it with a custom grid binding (knockout has a feature to create own binding
+  so we use one called <<<simpleGrid>>> which will display grids.
+
+  Grid view initialisation code (some details omitted) :
+
++----------------
+    var managedRepositoriesViewModel = new ManagedRepositoriesViewModel();
+
+    $.ajax("restServices/archivaServices/managedRepositoriesService/getManagedRepositories", {
+        type: "GET",
+        dataType: 'json',
+        success: function(data) {
+          // data mapping json -> javascript
+          managedRepositoriesViewModel.managedRepositories(mapManagedRepositories(data));
+          // we define here our grid view model for fields only displayed
+          managedRepositoriesViewModel.gridViewModel = new ko.simpleGrid.viewModel({
+            data: managedRepositoriesViewModel.managedRepositories,
+            columns: [
+              {
+                headerText: $.i18n.prop('identifier'),
+                rowText: "id"
+              },
+              {
+                headerText: $.i18n.prop('name'),
+                rowText: "name"
+              },
+              {
+              headerText: $.i18n.prop('type'),
+              rowText: "getTypeLabel",
+              // FIXME i18n
+              title: "Repository type (default is Maven 2)"
+              }
+            ],
+            // max items per size, the binding has a pagination feature
+            pageSize: 5,
+            // we can define here a callback function which be called on all grid change (adding/updating/removing values from the array)
+            gridUpdateCallBack: function(){
+              $("#main-content #managed-repositories-table [title]").twipsy();
+            }
+          });
+          // apply the binding on the specified node
+          ko.applyBindings(managedRepositoriesViewModel,$("#main-content #managed-repositories-table").get(0));
+        }
+      }
+    );
++----------------
+
+* View definition
+
+** Binding definition
+
+  We have applyed binding on the node with id "#managed-repositories-table". The binding definition is:
+
++----------------
+  <table class="bordered-table zebra-striped" id="managed-repositories-table"
+         data-bind="simpleGrid: gridViewModel,simpleGridTemplate:'ko_managed-repositoriesGrid',pageLinksId:'managed-repositoriesPagination',data:'managedRepositories'">
+  </table>
++----------------
+
+  * simpleGrid: gridViewModel = field name for the view model (see sample above)
+
+  * simpleGridTemplate:'ko_managed-repositoriesGrid' = name of the template to use (see below)
+
+  * pageLinksId:'managed-repositoriesPagination' = name of the template to use for pagination.
+
+  * data:'managedRepositories' = fields which contains data to pass to pass to the template
+
+** Template content
+
+  Template used for grid display (some details omitted).
+
++----------------
+<script id='ko_managed-repositoriesGrid' type='text/x-jquery-tmpl'>
+  <thead>
+      <tr>
+        // display read only fields defined in ko.simpleGrid.viewModel.columns (see above)
+        {{each(i, columnDefinition) columns}}
+          <th title="${ columnDefinition.title }">${ columnDefinition.headerText }</th>
+        {{/each}}
+        <th>Releases</th>
+        .....
+        // custom columns
+        <th>${$.i18n.prop('edit')}</th>
+        <th>${$.i18n.prop('delete')}</th>
+      </tr>
+  </thead>
+  <tbody>
+      {{each(i, row) itemsOnCurrentPage()}}
+          <tr>
+            // display read only fields defined in ko.simpleGrid.viewModel.columns (see above)
+            {{each(j, columnDefinition) columns}}
+              <td>${ typeof columnDefinition.rowText == 'function' ? columnDefinition.rowText(row) : row[columnDefinition.rowText] }</td>
+            {{/each}}
+            // custom columns which images depending on a value
+            <td>
+            {{if row.releases() == true}}
+              <img src="images/weather-clear.png" title="${$.i18n.prop('release.included')}"/>
+            {{else}}
+              <img src="images/dialog-error.png" title="${$.i18n.prop('release.notincluded')}"/>
+            {{/if}}
+            </td>
+            .....
+            // custom columns with binding mapped to ManagedRepositoriesViewModel methods
+            <td><a href="#" data-bind="click: function(){ editManagedRepository(row) }">${$.i18n.prop('edit')}</a></td>
+            <td>
+              <a href="#" data-bind="click: function(){ removeManagedRepository(row) }">
+                <img src="images/edit-cut.png" title="${$.i18n.prop('delete')}"/>
+              </a>
+            </td>
+            .....
+          </tr>
+      {{/each}}
+  </tbody>
+
++----------------
index f0ce695fcc71ecc347a6b87253f0b3d4f1d411c8..0ac32fd8f3e54e586ac149fc3f3572e182286822 100644 (file)
@@ -111,19 +111,20 @@ Support single value:
           <ul>
             <li>
               One tab/pill display the grid
-              <a href="images/repo-grids.png" class="single_image" title="Start Page">
+              <a href="images/repo-grids.png" class="single_image" title="Repositories Grid">
                 <img src="images/repo-grids.png" width="150" height="93" style="border: 1px solid silver; margin-left: 0.25em; margin-bottom: 0.25em" alt="" /><br/>
                 Repositories Grid
               </a>
             </li>
             <li>
               One tab/pill display the edit/add form. <b>As we use knockout framework grid will be updated with new value from add/edit form automatically.</b>
-              <a href="images/repo-edit.png" class="single_image" title="Start Page">
+              <a href="images/repo-edit.png" class="single_image" title="Repositories Edit/Add">
                 <img src="images/repo-edit.png" width="150" height="93" style="border: 1px solid silver; margin-left: 0.25em; margin-bottom: 0.25em" alt="" /><br/>
                 Repositories Edit/Add
               </a>
             </li>
           </ul>
+          Have a look at the page <a href="./knockout-binding.html">knockout-binding</a> for more explanation.
         </p>
       </subsection>