1

リストを処理するコントローラーとファクトリーがあります。コントローラは、ファクトリによってロードされたリストを取得し、それをビューに表示する必要があります。これは FireBase から非同期的にロードする必要があるため、ファクトリに getLists() メソッドを含めることはできません。これが私のコントローラーコードです-

angular.module('myApp.controllers', []).
  controller('ListCtrl', ["$scope","listFactory", function($scope, ListFactory) {
    $scope.lists = [];

    $scope.$on("list_update", function(snapshot)
    {
        console.log(snapshot);
    });

  }]).
  controller("EditListCtrl", ["$scope","listFactory", function($scope, ListFactory)
    {
        $scope.name = "";
        $scope.items = [];
        $scope.itemCount = 10;

        $scope.save = function()
        {
            var List = {"items":[]};
            for(var i = 0; i < $scope.itemCount; i++)
            {
                var item = $scope.items[i];
                if(item != null)
                {
                    List.items.push(item);
                }
                else
                {
                    alert("Please fill all items of the list.");
                    return false;
                }

                ListFactory.putList(List);
                $scope.items = [];
                $scope.name = "";
            }
        }
    }]);

listFactory は次のようになります。

angular.module("myApp.factories", [])
    .factory("listFactory", [function()
    {
        var lists = [{"name":"test"}];
        var ListRef = new Firebase("https://listapp.firebaseio.com/");

        var factory = {};
        factory.getLists = function()
        {
            // this won't work
        }

        factory.putList = function(List)
        {
            ListRef.child("lists").push(List);
        }

        ListRef.on("child_added", function(snapshot)
        {
            // How do I get this back to the controller???
        });

        return factory;
    }]);

ListRef は、スナップショット引数がリスト データを持つ「child_added」イベントを送出します。どうにかしてこれをコントローラーに戻す必要があります。イベントでこれを行いたいのですが、ファクトリとコントローラーの間でそれを行う方法がわかりません。ルート スコープを使用したくないのは、それが悪い習慣だと思うからです。

私はこれに不慣れです-どんな助けもいただければ幸いです!

4

1 に答える 1