在AngularJS控制器,我有以下定義: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
,並從dataSourceFactory
傳遞給abstractDataFactory
這些參數。
注入新dataSourceFactory
到我的控制器之後,它會返回不同的數據源,根據不同的方法調用:
var dataSourceFactory = new dataSourceFactory("/odata/ContentType");
var dataSource = dataSourceFactory.contentType(); // .userDetails(), .someOtherData()...
據我瞭解,角工廠恢復功能,所以我不認爲這正是我正在尋找的東西。
到目前爲止,這是我不工作的落實:
控制器:
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
...
DataSourceFactory:
// 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
中定義各種數據源,並如上所述使用。這是推薦的(我要去重複使用代碼,而不是針對每個數據源的一堆單獨的工廠),如果不是,建議?
通常,服務是單例,工廠按需創建實例。 –
因此,我的初始設置,我相信是正確的 - 我正在創建一個具有特定「oDataUrl」值的'dataSourceFactory'的新按需實例。 – ElHaix
工廠不必返回函數,他們可以返回任何你想要的 – doodeec