1

我正在努力将 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("/");
});

接下来我设置工厂: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
    };
})

控制器: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 上设置了一个断点,然后我看到了呼叫。我还看到查询运行并返回数据,但是当我查看从工厂返回的 ArtLogDataService 对象时,它总是显示 Array[0]。数据似乎永远不会绑定到 artLogView。

我究竟做错了什么?

谢谢!

4

2 回答 2

1

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

$scope.$apply()当您的数据返回时,您需要拨打电话。这将导致绑定注意到数据的变化并进行更新。

也许是这样的:

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

如果你在 Angular 中做任何事情,你永远不需要调用$scope.$apply,因为任何可以改变数据(事件、网络响应、超时等)的东西都会被 Angular 处理(通过$http$timeout等)并$apply会自动被调用。正是在这些情况下,数据被来自 Angular 之外的事件更改$scope.$apply是必要的。

希望这对你有用!

于 2013-09-03T19:44:40.200 回答
0

您不...也不应该...在查询回调中使用 $q.deferred。当您使用Breeze.Angular模块时,Breeze EntityManager 方法已经返回了 Promise ... $q 承诺,如文档中所述,并在“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
    }
};
于 2014-09-03T19:23:51.563 回答