jaystack / jaydata

Notice: this library isn't maintained anymore
http://jaydata.org
GNU General Public License v2.0
352 stars 95 forks source link

Support for inlinecount with Knockout.js extension #80

Open khaledh opened 11 years ago

khaledh commented 11 years ago

JayData 1.2.6 introduced support for inline counts with OData, using the withInlineCount() method, which adds a totalCount property to the returned items array.

Unfortunately the knockout.js module does not have similar support.

I was able to modify the knockout.js extension a little to support inline counts by adding a second parameter to its toArray() method, which should be an observable. The method checks if totalCount exists on the results array, and if it exists it sets the observable parameter to its value.

agdolla commented 11 years ago

please attach the patch here for review or send a pull request

khaledh commented 11 years ago
--- packages/JayData.1.2.6/content/Scripts/jaydatamodules/knockout.js   Thu Jan 17 01:39:14 2013
+++ packages/JayData.1.2.6/content/Scripts/jaydatamodules/knockout.js   Thu Jan 24 00:58:28 2013
@@ -384,13 +384,15 @@
         };

         var queryableToArray = $data.Queryable.prototype.toArray;
-        $data.Queryable.prototype.toArray = function (onResult_items) {
+        $data.Queryable.prototype.toArray = function (onResult_items, onResult_totalCount) {
             if (ko.isObservable(onResult_items)) {
                 if (typeof onResult_items.push !== 'undefined') {
                     var callBack = $data.typeSystem.createCallbackSetting();

                     return this.toArray(function (results) {
-                        onResult_items([]);
+                       if (typeof results.totalCount === 'number' && ko.isObservable(onResult_totalCount))
+                           onResult_totalCount(results.totalCount);
+                       onResult_items([]);
                         results.forEach(function (result, idx) {
                             if (result instanceof $data.Entity) {
                                 onResult_items.push(result.asKoObservable());