使用BreezeJs从角度工厂获取数据

时间:2013-09-03 19:17:57

标签: javascript angularjs breeze

我正在努力将Angular工厂实施到我正在开发的项目中。

我已经开始工作:ArtLogMain.js

var ArtLog = angular.module('ArtLog', ['ngGrid', 'ui.bootstrap']);

ArtLog.config(function ($locationProvider, $routeProvider) {
    $locationProvider.html5Mode(true);

    $routeProvider.when("/ArtLog", {
        controller: "ArtLogCtrl",
        templateUrl: "/Templates/ArtLog/Index.html"
    });

    $routeProvider.when("/ArtLog/:Id", {
        controller: "ArtLogEditCtrl",
        templateUrl: "/Templates/ArtLog/Edit.html"
    });

    $routeProvider.when("/ArtLog/Dashboard", {
        controller: "ArtLogDashBoardCtrl",
        templateUrl: "/Templates/ArtLog/Dashboard.html"
    });

    $routeProvider.otherwise("/");
});

接下来我设置Factory:ArtLogDataService

ArtLog.factory("ArtLogDataService", function ($q) {
    breeze.config.initializeAdapterInstance("modelLibrary", "backingStore", true);

    var _artLogView = [];
    var _artLogSingle = [];

    var _getArtLogById = function (Id) {

        var deferred = $q.defer();

        var manager = new breeze.EntityManager('breeze/BreezeData');
        var query = new breeze.EntityQuery().from('Project').where("Id", "Equals", Id);

        manager.executeQuery(query).then(function (data) {
            angular.copy(data, _artLogSingle);
            deferred.resolve();
        }).fail(function () {
            deferred.reject();
        });

        return deferred.promise;
    };

    var _getArtLogView = function () {

        var deferred = $q.defer();

        var manager = new breeze.EntityManager('breeze/BreezeData');
        var query = new breeze.EntityQuery().from('ArtLogView');

        manager.executeQuery(query).then(function (data) {
            //angular.copy(data.results, _artLogView);
            _artLogView = data.results;
            deferred.resolve();
        }).fail(function () {
            deferred.reject();
        });

        return deferred.promise;
    };

    return {
        artLogView: _artLogView,
        artLogSingle: _artLogSingle,
        getArtLogView: _getArtLogView,
        getArtLogById: _getArtLogById
    };
})

Controller:ArtLogController.js

function ArtLogCtrl($scope, ArtLogDataService) {
    $scope.ArtLogData = ArtLogDataService;
    $scope.editableInPopup = '<button id="editBtn" type="button" class="btn btn-primary" ng-click="edit(row)" >Edit</button>';

    ArtLogDataService.getArtLogView();

    $scope.edit = function (row) {
        window.location.href = '/ArtLog/' + row.entity.Id;
    };

    $scope.gridOptions = {
        data: ArtLogDataService.artLogView,
        showGroupPanel: true,
        enablePinning: true,
        showFilter: true,
        multiSelect: false,
        columnDefs: [
            { displayName: 'Edit', cellTemplate: $scope.editableInPopup, width: 80, pinned: true, groupable: false, sortable: false },
            { field: 'ArtNum', displayName: 'Art Number', resizable: true, pinned: true, groupable: false, width: '100px' },
            { field: 'CreateDate', displayName: 'Date Created', cellFilter: "date:'MM-dd-yyyy'", pinnable: false, width: '110px' },
            { field: 'ArtCompletionDue', displayName: 'Art Comp Due Date', cellFilter: "date:'MM-dd-yyyy'", pinnable: false, width: '160px', enableCellEdit: true },
            { field: 'DaysLeft', displayName: 'Days Left', pinnable: false, width: '90px' },
            { field: 'RevisionNum', displayName: 'Rev Number', pinnable: false, width: '100px' },
            { field: 'Status', displayName: 'Status', pinnable: false, width: '80px' },
            { field: 'Template', displayName: 'Template', pinnable: false, width: '190px' },
            { field: 'Driver', displayName: 'Driver', pinnable: false, width: '160px' },
            { field: 'AssignedTo', displayName: 'Assigned To', pinnable: false, width: '160px' },
            { field: 'BuddyArtist', displayName: 'Buddy Artist', pinnable: false, width: '160px' }
        ],
        filterOptions: {
            filterText: "",
            useExternalFilter: false
        }
    };
}

我在ArtLogDataService.getArtLogData上设置断点,我看到了调用fire。我还看到查询运行并返回数据,但是当我查看从工厂返回的ArtLogDataService对象时,它总是显示Array [0]。数据似乎永远不会绑定到artLogView。

我做错了什么?

谢谢!

2 个答案:

答案 0 :(得分:1)

问题是来自Breeze的网络回调不是Angular更新循环的一部分。 Angular不知道您的数据发生了变化,因此视图绑定上的观察者永远不会更新。

当您的数据恢复时,您需要接听$scope.$apply()电话。这将导致绑定注意到数据的更改并进行更新。

也许是这样的:

ArtLogDataService.getArtLogView().then(function() {
    $scope.$apply();
});

如果您在Angular中执行所有操作,则永远不需要调用$scope.$apply,因为任何可以改变数据(事件,网络响应,超时等)的内容都将由Angular处理(通过$http$timeout等)和$apply将自动被调用。正是在这些情况下,来自Angular之外的事件改变了数据$scope.$apply是必要的。

希望这能为你做到!

答案 1 :(得分:0)

你不......并且不应该......在查询回调中使用$ q.deferred。当您使用 Breeze.Angular 模块时,Breeze EntityManager方法已经返回promises ... $ q promises,如文档中所述,并在“Todo-Angular”等示例中进行了演示。

摆脱你手绘的承诺,你也不需要$ apply。

应该是这样的:

// Create or acquire the manager ONCE for the lifetime of your data service
// Todo: more testable to use a separate "entityManagerFactory" service
//       to get your manager.
var manager = new breeze.EntityManager('breeze/BreezeData');

var _getArtLogView = function () {

    return breeze.EntityQuery.from('ArtLogView')
                 .using(manager).execute()
                 .then(success)
                 .catch(fail); // only if you have something useful to do here when it fails

    function success(data) { return data.results; }

    function fail(error) { 
       // some kind of logging or useful error handling;
       // otherwise don't bother with fail here
       return $q.reject(error); // so caller sees it
    }
};