如何从自定义Kendo UI数据源访问Javascript函数
我正在使用Kendo UI和AngularJS 1.x.我还在使用Firebase和AngularFire库。因为我使用的是Firebase,所以我决定扩展Kendo UI DataSource,我将其命名为kendo.data.FirebaseDataSource.js。如何从自定义Kendo UI数据源访问Javascript函数
我的自定义DataSource扩展了Kendo UI数据源,并添加了我想要访问AngularFire功能的自定义传输。
例如,在kendo.data.FirebaseDataSource.js
源,我想访问AngularFire的$ firebaseArray功能,还我已创建的任何其他模块(效用函数等)驻留在名为my-custom-module.js
另一个文件中,但我无法看到我是怎么可以这样做。
我试着做以下,但它(显然)不工作:
(function ($, kendo, firebase, $firebaseArray) {
'use strict';
编辑
kendo.data.FirebaseDataSource.js
(function ($, kendo, firebase, $firebaseArray) {
'use strict';
var getCustomer = function() {
var data = $firebaseArray(firebaseDataService.data);
var listed = [];
data.$loaded()
.then(function(){
angular.forEach(data, function(data){
listed.push({
"firstName": data.firstName,
"lastName": data.lastName
})
})
})
return listed;
};
var firebaseTransports = {
read: function (options) {
var customers = getCustomers();
options.success(customers);
}
};
kendo.data.extensions.FirebaseDataSource = kendo.data.DataSource.extend({
init: function (options) {
kendo.data.DataSource.fn.init.call(this, $.extend(true, {}, { transport: firebaseTransports }, options));
}
});
})($, kendo, firebase, $firebaseArray);
EDIT 2
controllerGrid.js
var fbDataSource = new kendo.data.extensions.FirebaseDataSource({
schema: {
model: {
id: "id",
fields: {
firstName: { type: "string" },
lastName: { type: "string" }
}
}
}
})
vm.gridOptions = {
dataSource: fbDataSource
基于上述controllerGrid.js
来源,我不得不为我的剑道UI网格控件做使用读传输功能的kendo.data.FirebaseDataSource.js
是只是让我gridOptions一个简单的引用到新创建的fbDataSource 。
$ firebaseArray是一种只存在于角度内的概念,因为它是一种注射剂。你不能创建一个函数并传递它来定义它。您必须使用本机Firebase调用,因为这些调用暴露在窗口中。
为了让你将不得不使用这样的数据:
firebase.database().ref(...).on('value', function(snapshot){
console.log(snapshot.val());
})
你可以阅读关于如何在这里使用它的文档:https://firebase.google.com/docs/database/web/read-and-write#read_data_once
如果你需要注入,你会必须让你的kendoUi在一个指令中,然后你可以用这种方式进行注入。
.directive(..., function(){
return {
...
controller: function($firebaseArray){
//kendo code here
}
}
})
谢谢。我想注入,但是我如何从我的其他函数中访问它,特别是从Kendo UI Grid小部件中访问?我添加了更多的源代码供参考。 – Ruepen
请分享小提琴或代码片段,这将有助于找出问题。 – Shrinath
@Shrinath新增了一些资源。 – Ruepen