如何在 $asArray 返回的列表中扩展返回的对象? [英] How to extend returned objects in the list returned by $asArray?

查看:22
本文介绍了如何在 $asArray 返回的列表中扩展返回的对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用新方法(不是装饰数组本身)装饰由 $asArray 在 angularfire 中返回的列表中的对象时遇到问题.

I'm having trouble decorate the objects in my list returned by $asArray in angularfire with a new method (not decorating the array itself).

angularfire 文档似乎表明,正确的方法是在工厂中为 $FirebaseArray 覆盖 $$ added 方法,返回一个新对象,该对象封装或扩展传递给该方法的快照.来自 文档:

The angularfire documentation seems to suggest that the right way to do this is to override the $$added method in the factory for $FirebaseArray, returning a new object that either encapsulates or extends the snapshot that gets passed in to that method. From the documentation:

// an object to return in our JokeFactory
app.factory("Joke", function($firebaseUtils) {
  function Joke(snapshot) {
    this.$id = snapshot.name();
    this.update(snapshot);
  }

  Joke.prototype = {
    update: function(snapshot) {
      // apply changes to this.data instead of directly on `this`
      this.data = snapshot.val();
    },

    makeJoke: function() {
      alert("Why did the " + this.animal + " cross the " + this.obstacle + "?");
    },

    toJSON: function() {
      // since we didn't store our data directly on `this`, we need to return
      // it in parsed format. We can use the util function to remove $ variables
      // and get it ready to ship
      return $firebaseUtils.toJSON(this.data);
    }
  };

  return Joke;
});

app.factory("JokeFactory", function($FirebaseArray, Joke) {
  return $FirebaseArray.$extendFactory({
    // change the added behavior to return Joke objects
    $$added: function(snap) {
      return new Joke(snap);
    },

    // override the update behavior to call Joke.update()
    $$updated: function(snap) {
       this.$getRecord(snap.name()).update(snap);
    }
  });
});

但是,当我在我的代码中执行此操作时,没有任何内容添加到数组中,尽管我可以从输出到控制台看到它正在被调用.

However, when I do this in my code, nothing ever gets added to the array, although I can see from outputting to the console that it is getting called.

var printMessageObjConstructor = function(snap) {
      this.$id = snap.name();
      this.snapshot = snap;
      this.$update = function(snap) {
        this.snapshot = snap;
      };
      this.printMessage = function() {
        return this.author + "'s question is: " + this.body;
      };
    };

    var ref = new Firebase("https://danculley-test.firebaseio.com/questions");
    //What Am I Doing Wrong Here?
    var arrayFactory = $FirebaseArray.$extendFactory({
      $$added: function(snap, prevChild) {
        var x = new printMessageObjConstructor(snap);
        console.log("I am being called from FirebaseDecoratedCtlOverloadAddedinNewObj.");
        return x;
      },
      $createObject: function(snap) {
        return new printMessageObjConstructor(snap);
      },
      $$updated: function(snap) {
        var i = this.$indexFor(snap.name());
        var q = this.$list[i];
        q.$update(snap);
      }
    });
    var sync = $firebase(ref, {arrayFactory:arrayFactory});

    var list = sync.$asArray();

    list.$loaded(function(list) {
      $scope.questions = list;
    });

我已经建立了一个新的 plunk 以通过其他几个用例来展示这个问题我试过的.(我添加的实际方法更复杂,与视图无关,但我想做一些简单的事情来重现问题.)

I've set up a new plunk stripped down to show the issue with a couple other use cases that I've tried. (The actual method I'm adding is more complex and isn't related to the view, but I wanted to do something simple to reproduce the issue.)

我认为问题在于我不太明白 $$ added 究竟应该返回什么,或者除了返回要存储的 $$ added 值之外还有哪些额外的行为.在原型或 $FirebaseArray 上似乎也没有真正添加 $$ 来作为超级调用以获得默认行为.有人能指出我正确的方向吗?

I think the issue is that I don't quite understand what exactly $$added is supposed to return, or what additional behavior beside returning the value to be stored $$added is supposed to have. There also doesn't really seem to be an $$added on the prototype or on $FirebaseArray to call as a super to get the default behavior. Can someone point me in the right direction?

更新

为了他人的利益,在查看了 Kato 发布的类似内容后,我能够通过添加以下内容来解决问题,除了下面的注释行外,几乎所有内容都是直接从源代码复制的.

For the benefit of others, after reviewing the like that Kato posted, I was able to solve the issue by adding the following, almost all copied directly from the source except for the commented line below.

$$added: function(snap, prevChild) {

        var i = this.$indexFor(snap.name());
          if( i === -1 ) {

            var rec = snap.val();
            if( !angular.isObject(rec) ) {
              rec = { $value: rec };
            }
            rec.$id = snap.name();
            rec.$priority = snap.getPriority();
            $firebaseUtils.applyDefaults(rec, this.$$defaults);

            //This is the line that I added to what I copied from the source
            angular.extend(rec, printMessageObj);


            this._process('child_added', rec, prevChild);
          }
      }

推荐答案

为了他人的利益,在查看了 Kato 发布的链接后,我能够通过添加以下内容来解决问题,几乎都是直接从源代码复制的除了下面的注释行.

For the benefit of others, after reviewing the link that Kato posted, I was able to solve the issue by adding the following, almost all copied directly from the source except for the commented line below.

$$added: function(snap, prevChild) {

    var i = this.$indexFor(snap.name());
      if( i === -1 ) {

        var rec = snap.val();
        if( !angular.isObject(rec) ) {
          rec = { $value: rec };
        }
        rec.$id = snap.name();
        rec.$priority = snap.getPriority();
        $firebaseUtils.applyDefaults(rec, this.$$defaults);

        //This is the line that I added to what I copied from the source
        angular.extend(rec, printMessageObj);


        this._process('child_added', rec, prevChild);
      }
  }

这篇关于如何在 $asArray 返回的列表中扩展返回的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆