在 AngularJS 控制器中,我定义了以下内容:
app.controller('contentTypeController', ['$scope', '$log', 'abstractDataFactory', 'customFunctions',
// the abstract data factory accepts controller type parameters for 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");
});
...
这一切都很好。但是,我不想dataSource
在另一个控制器中重新定义这个特定的数据集 - ContentType,并且想将其抽象出来。
结果,我创建了一个新的dataSourceFactory
. 我并不完全清楚或实施此的最佳策略是什么。
我在想我想像我从控制器中一样的方式进行更新,并将这些参数传递给从.dataSourceFactory
abstractDataFactory
abstractDataFactory
dataSourceFactory
将新的dataSourceFactory
注入我的控制器后,它将返回各种数据源,具体取决于方法调用:
var dataSourceFactory = new dataSourceFactory("/odata/ContentType");
var dataSource = dataSourceFactory.contentType(); // .userDetails(), .someOtherData()...
据我了解,Angular 工厂返回函数,所以我不认为这正是我想要的。
到目前为止,这是我的非工作实现:
控制器:
app.controller('contentTypeController', ['$scope', '$log', 'dataSourceFactory', 'customFunctions',
function ($scope, $log, dataSourceFactory, customFunctions) {
var dataSourceFactory = new dataSourceFactory("/odata/ContentType");
var dataSource = dataSourceFactory.contentTypes(); // returns a function, rather than kendo.data.DataSource object
...
数据源工厂:
// factory to 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);
// do something with 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;
});
总之,
var dataSource = dataSourceFactory.contentTypes(); // returns a function, rather than kendo.data.DataSource object
1)数据源必须是新kendo.data.DataSource
对象的值,而不是函数。由于工厂返回该功能,我如何在我的控制器中使用它,这是正确的方法吗?如果不是建议?
2)我将在dataSourceFactory
上面定义和使用的各种数据源。是否建议这样做(我要重用代码,而不是为每个数据源创建一堆单独的工厂),如果不是,建议?