javascript - AngularJS : How to return a reusable object from a factory? -


in angularjs controller, have following defined:

app.controller('contenttypecontroller', ['$scope', '$log', 'abstractdatafactory', 'customfunctions',     // abstract data factory accepts controller type parameters restful crud      function ($scope, $log, abstractdatafactory, customfunctions) {          var datafactory = new abstractdatafactory("/odata/contenttype");          var crudservicebaseurl = "/odata/contenttype";          var datasource = new kendo.data.datasource({             type: "odata",             transport: {                 read:                      function (options) {                         var odataparams = kendo.data.transports["odata"].parametermap(options.data, "read");                          datafactory.getlist(odataparams)                             .success(function (result) {                                 options.success(result);                             })                             .error (function (error) {                                 console.log("data error");                             });                   ... 

this works fine. however, don't want have redefine datasource in controller, particular data set - contenttype, , abstract out.

as result, created new datasourcefactory. i'm not entirely clear on or best strategy implementing this.

i thinking new datasourcefactory same way did abstractdatafactory controller, , parameters passed on abstractdatafactory datasourcefactory.

after injecting new datasourcefactory controller, return various data sources, depending on method call:

var datasourcefactory = new datasourcefactory("/odata/contenttype"); var datasource = datasourcefactory.contenttype();  // .userdetails(), .someotherdata()... 

from understand, angular factories return functions, don't think i'm looking for.

so far, here's non-working implementation:

controller:

app.controller('contenttypecontroller', ['$scope', '$log', 'datasourcefactory', 'customfunctions',      function ($scope, $log, datasourcefactory, customfunctions) {          var datasourcefactory = new datasourcefactory("/odata/contenttype");         var datasource = datasourcefactory.contenttypes();  // returns function, rather kendo.data.datasource object         ... 

datasourcefactory:

// factory return datasources app.factory('datasourcefactory', function (abstractdatafactory) {      function datasourcefactory(odataurlbase) {         this.datafactory = new abstractdatafactory(odataurlbase);     }      datasourcefactory.prototype = {         contenttypes: function () {             new kendo.data.datasource({                 type: "odata",                 transport: {                     read:                          function (options) {                             var odataparams = kendo.data.transports["odata"].parametermap(options.data, "read");                              this.datafactory.getlist(odataparams)                                 .success(function (result) {                                     options.success(result);                                 })                                 .error(function (error) {                                     console.log("data error");                                 });                          }                },                 batch: false,                 pagesize: 10,                 serverpaging: true,                 change: function (e) {                     console.log("change: " + e.action);                     // e                 },                 schema: {                     data: function (data) {                         //console.log(data)                         return data.value;                     },                     total: function (data) {                         console.log("count: " + data["odata.count"]);                         return data["odata.count"];                     },                     model: {                         id: "contenttypeid",                         fields: {                             contenttypeid: { editable: false, nullable: true },                             //userid: {editable: false, nullable: false },                             description: { type: "string", validation: { required: true } },                             //msrepl_tran_version: { type: "string", validation: { required: true } }                         }                     }                 },                 error: function (e) {                     //var response = json.parse(e.responsetext);                     var response = e.status;                     console.log(response);                 }             })  // datasource         }  // contenttypes     };      return datasourcefactory;  }); 

in summary,

var datasource = datasourcefactory.contenttypes();  // returns function, rather kendo.data.datasource object      

1) data source has value of new kendo.data.datasource object, rather function. since factory returns function, how can make use of in controller, , right way of going this, , if not suggestions?

2) have various data sources defined in datasourcefactory , used mentioned above. recommended (i'm going code reuse, , not bunch of separate factories each data source), , if not, suggestions?

thanks subtle nudge comments, able figure out.

the first issue wasn't returning function call (missing return statement contenttypes:).

secondly, had define datafactory inside datasourcefactory represented abstractdatafactory:

app.factory('datasourcefactory', function (abstractdatafactory) {     var datafactory;      function datasourcefactory(odataurlbase) {         datafactory = new abstractdatafactory(odataurlbase);     }      datasourcefactory.prototype = {         contenttypes: function () {             return new kendo.data.datasource({ ... }... 

what i'm still not clear on, if way go - having data source objects returned 1 factory.


Comments