【问题标题】:EmberFire and nested arraysEmberFire 和嵌套数组
【发布时间】:2014-01-11 05:16:26
【问题描述】:

我正在尝试我的第一个基于 ember 的项目,并将 emberfire 集成到其中以获得 Firebase 功能。我正在为另一个项目构建一个树形菜单构建器。

firebase数据库的json如下:

    {
  "default" : {
    "_type" : "arrayObject",
    "name" : "unnamed",
    "_name" : "default",
    "menus" : {
      "Top" : {
        "menu" : {
          "2" : {
            "text" : "0-2",
            "actions" : {
              "_type" : "objectArray",
              "0" : {
                "action" : 3,
                "_type" : "object"
              }
            },
            "column" : 0,
            "color" : {
              "b" : {
                "_type" : "object"
              },
              "_type" : "objectArray",
              "g" : 0,
              "a" : {
                "_type" : "object"
              },
              "r" : 0
            },
            "_type" : "object",
            "visibility" : "hidden",
            "page" : 0,
            "bgColor" : {
              "b" : {
                "_type" : "object"
              },
              "_type" : "objectArray",
              "g" : 208,
              "a" : {
                "_type" : "object"
              },
              "r" : 208
            },
            "row" : 2,
            "id" : 2
          },
          "5" : {
            "text" : "0-5",
            "actions" : [ {
              "action" : 3
            } ],
            "column" : 0,
            "color" : {
              "b" : 0,
              "g" : 0,
              "a" : 255,
              "r" : 0
            },
            "_type" : "object",
            "visibility" : "hidden",
            "page" : 0,
            "bgColor" : {
              "b" : 208,
              "g" : 208,
              "a" : 255,
              "r" : 208
            },
            "row" : 5,
            "id" : 5
          },
          "12" : {
            "text" : "1-3",
            "actions" : {
              "_type" : "objectArray",
              "0" : {
                "action" : 3,
                "_type" : "object"
              }
            },
            "column" : 1,
            "color" : {
              "b" : {
                "_type" : "object"
              },
              "_type" : "objectArray",
              "g" : {
                "_type" : "object"
              },
              "a" : {
                "_type" : "object"
              },
              "r" : {
                "_type" : "object"
              }
            },
            "_type" : "object",
            "visibility" : "hidden",
            "page" : 0,
            "bgColor" : {
              "b" : {
                "_type" : "object"
              },
              "_type" : "objectArray",
              "g" : {
                "_type" : "object"
              },
              "a" : {
                "_type" : "object"
              },
              "r" : {
                "_type" : "object"
              }
            },
            "row" : 3,
            "id" : 12
          },
          "15" : {
            "text" : "1-6",
            "actions" : {
              "_type" : "objectArray",
              "0" : {
                "action" : 3,
                "_type" : "object"
              }
            },
            "column" : 1,
            "color" : {
              "b" : {
                "_type" : "object"
              },
              "_type" : "objectArray",
              "g" : {
                "_type" : "object"
              },
              "a" : {
                "_type" : "object"
              },
              "r" : {
                "_type" : "object"
              }
            },
            "_type" : "object",
            "visibility" : "hidden",
            "page" : 0,
            "bgColor" : {
              "b" : {
                "_type" : "object"
              },
              "_type" : "objectArray",
              "g" : {
                "_type" : "object"
              },
              "a" : {
                "_type" : "object"
              },
              "r" : {
                "_type" : "object"
              }
            },
            "row" : 6,
            "id" : 15
          },
          "8" : {
            "text" : "0-8",
            "actions" : {
              "_type" : "objectArray",
              "0" : {
                "action" : 3,
                "_type" : "object"
              }
            },
            "column" : 0,
            "color" : {
              "b" : {
                "_type" : "object"
              },
              "_type" : "objectArray",
              "g" : {
                "_type" : "object"
              },
              "a" : {
                "_type" : "object"
              },
              "r" : {
                "_type" : "object"
              }
            },
            "_type" : "object",
            "visibility" : "hidden",
            "page" : 0,
            "bgColor" : {
              "b" : {
                "_type" : "object"
              },
              "_type" : "objectArray",
              "g" : {
                "_type" : "object"
              },
              "a" : {
                "_type" : "object"
              },
              "r" : {
                "_type" : "object"
              }
            },
            "row" : 8,
            "id" : 8
          },
          "_type" : "objectArray",
          "7" : {
            "text" : "0-7",
            "actions" : [ {
              "action" : 3
            } ],
            "column" : 0,
            "color" : {
              "b" : 0,
              "g" : 0,
              "a" : 255,
              "r" : 0
            },
            "_type" : "object",
            "visibility" : "visible",
            "page" : 0,
            "bgColor" : {
              "b" : 208,
              "g" : 208,
              "a" : 255,
              "r" : 208
            },
            "row" : 7,
            "id" : 7
          },
          "17" : {
            "text" : "1-8",
            "actions" : {
              "_type" : "objectArray",
              "0" : {
                "action" : 3,
                "_type" : "object"
              }
            },
            "column" : 1,
            "color" : {
              "b" : {
                "_type" : "object"
              },
              "_type" : "objectArray",
              "g" : {
                "_type" : "object"
              },
              "a" : {
                "_type" : "object"
              },
              "r" : {
                "_type" : "object"
              }
            },
            "_type" : "object",
            "visibility" : "hidden",
            "page" : 0,
            "bgColor" : {
              "b" : {
                "_type" : "object"
              },
              "_type" : "objectArray",
              "g" : {
                "_type" : "object"
              },
              "a" : {
                "_type" : "object"
              },
              "r" : {
                "_type" : "object"
              }
            },
            "row" : 8,
            "id" : 17
          },
          "1" : {
            "text" : "0-1",
            "actions" : {
              "_type" : "objectArray",
              "0" : {
                "action" : 3,
                "_type" : "object"
              }
            },
            "column" : 0,
            "color" : {
              "b" : {
                "_type" : "object"
              },
              "_type" : "objectArray",
              "g" : 0,
              "a" : {
                "_type" : "object"
              },
              "r" : 0
            },
            "_type" : "object",
            "visibility" : "hidden",
            "page" : 0,
            "bgColor" : {
              "b" : {
                "_type" : "object"
              },
              "_type" : "objectArray",
              "g" : 208,
              "a" : {
                "_type" : "object"
              },
              "r" : 208
            },
            "row" : 1,
            "id" : 1
          },
          "4" : {
            "text" : "0-4",
            "actions" : {
              "_type" : "objectArray",
              "0" : {
                "action" : 3
              }
            },
            "column" : 0,
            "color" : {
              "b" : 0,
              "g" : 0,
              "a" : 255,
              "r" : 0
            },
            "_type" : "object",
            "visibility" : "hidden",
            "page" : 0,
            "bgColor" : {
              "b" : 208,
              "g" : 208,
              "a" : 255,
              "r" : 208
            },
            "row" : 4,
            "id" : 4
          },
          "11" : {
            "text" : "1-2",
            "actions" : {
              "_type" : "objectArray",
              "0" : {
                "action" : 3,
                "_type" : "object"
              }
            },
            "column" : 1,
            "color" : {
              "b" : {
                "_type" : "object"
              },
              "_type" : "objectArray",
              "g" : {
                "_type" : "object"
              },
              "a" : {
                "_type" : "object"
              },
              "r" : {
                "_type" : "object"
              }
            },
            "_type" : "object",
            "visibility" : "hidden",
            "page" : 0,
            "bgColor" : {
              "b" : {
                "_type" : "object"
              },
              "_type" : "objectArray",
              "g" : {
                "_type" : "object"
              },
              "a" : {
                "_type" : "object"
              },
              "r" : {
                "_type" : "object"
              }
            },
            "row" : 2,
            "id" : 11
          },
          "14" : {
            "text" : "1-5",
            "actions" : {
              "_type" : "objectArray",
              "0" : {
                "action" : 3,
                "_type" : "object"
              }
            },
            "column" : 1,
            "color" : {
              "b" : {
                "_type" : "object"
              },
              "_type" : "objectArray",
              "g" : {
                "_type" : "object"
              },
              "a" : {
                "_type" : "object"
              },
              "r" : {
                "_type" : "object"
              }
            },
            "_type" : "object",
            "visibility" : "hidden",
            "page" : 0,
            "bgColor" : {
              "b" : {
                "_type" : "object"
              },
              "_type" : "objectArray",
              "g" : {
                "_type" : "object"
              },
              "a" : {
                "_type" : "object"
              },
              "r" : {
                "_type" : "object"
              }
            },
            "row" : 5,
            "id" : 14
          },
          "6" : {
            "text" : "0-6",
            "actions" : [ {
              "action" : 3
            } ],
            "column" : 0,
            "color" : {
              "b" : 0,
              "g" : 0,
              "a" : 255,
              "r" : 0
            },
            "_type" : "object",
            "visibility" : "visible",
            "page" : 0,
            "bgColor" : {
              "b" : 208,
              "g" : 208,
              "a" : 255,
              "r" : 208
            },
            "row" : 6,
            "id" : 6
          },
          "0" : {
            "text" : "0-0",
            "actions" : {
              "_type" : "objectArray",
              "0" : {
                "action" : 3,
                "_type" : "object"
              }
            },
            "column" : 0,
            "color" : {
              "b" : {
                "_type" : "object"
              },
              "_type" : "objectArray",
              "g" : {
                "_type" : "object"
              },
              "a" : {
                "_type" : "object"
              },
              "r" : 0
            },
            "_type" : "object",
            "visibility" : "hidden",
            "page" : 0,
            "bgColor" : {
              "b" : {
                "_type" : "object"
              },
              "_type" : "objectArray",
              "g" : {
                "_type" : "object"
              },
              "a" : {
                "_type" : "object"
              },
              "r" : 208
            },
            "row" : 0,
            "id" : "0"
          },
          "9" : {
            "text" : "1-0",
            "actions" : {
              "_type" : "objectArray",
              "0" : {
                "action" : 3,
                "_type" : "object"
              }
            },
            "column" : 1,
            "color" : {
              "b" : {
                "_type" : "object"
              },
              "_type" : "objectArray",
              "g" : {
                "_type" : "object"
              },
              "a" : {
                "_type" : "object"
              },
              "r" : {
                "_type" : "object"
              }
            },
            "_type" : "object",
            "visibility" : "hidden",
            "page" : 0,
            "bgColor" : {
              "b" : {
                "_type" : "object"
              },
              "_type" : "objectArray",
              "g" : {
                "_type" : "object"
              },
              "a" : {
                "_type" : "object"
              },
              "r" : {
                "_type" : "object"
              }
            },
            "row" : 0,
            "id" : 9
          },
          "16" : {
            "text" : "1-7",
            "actions" : {
              "_type" : "objectArray",
              "0" : {
                "action" : 3,
                "_type" : "object"
              }
            },
            "column" : 1,
            "color" : {
              "b" : {
                "_type" : "object"
              },
              "_type" : "objectArray",
              "g" : {
                "_type" : "object"
              },
              "a" : {
                "_type" : "object"
              },
              "r" : {
                "_type" : "object"
              }
            },
            "_type" : "object",
            "visibility" : "hidden",
            "page" : 0,
            "bgColor" : {
              "b" : {
                "_type" : "object"
              },
              "_type" : "objectArray",
              "g" : {
                "_type" : "object"
              },
              "a" : {
                "_type" : "object"
              },
              "r" : {
                "_type" : "object"
              }
            },
            "row" : 7,
            "id" : 16
          },
          "3" : {
            "text" : "0-3",
            "actions" : {
              "_type" : "objectArray",
              "0" : {
                "action" : 3,
                "_type" : "object"
              }
            },
            "column" : 0,
            "color" : {
              "b" : 0,
              "_type" : "objectArray",
              "g" : 0,
              "a" : {
                "_type" : "object"
              },
              "r" : 0
            },
            "_type" : "object",
            "visibility" : "hidden",
            "page" : 0,
            "bgColor" : {
              "b" : 208,
              "_type" : "objectArray",
              "g" : 208,
              "a" : {
                "_type" : "object"
              },
              "r" : 208
            },
            "row" : 3,
            "id" : 3
          },
          "10" : {
            "text" : "1-1",
            "actions" : {
              "_type" : "objectArray",
              "0" : {
                "action" : 3,
                "_type" : "object"
              }
            },
            "column" : 1,
            "color" : {
              "b" : {
                "_type" : "object"
              },
              "_type" : "objectArray",
              "g" : {
                "_type" : "object"
              },
              "a" : {
                "_type" : "object"
              },
              "r" : {
                "_type" : "object"
              }
            },
            "_type" : "object",
            "visibility" : "hidden",
            "page" : 0,
            "bgColor" : {
              "b" : {
                "_type" : "object"
              },
              "_type" : "objectArray",
              "g" : {
                "_type" : "object"
              },
              "a" : {
                "_type" : "object"
              },
              "r" : {
                "_type" : "object"
              }
            },
            "row" : 1,
            "id" : 10
          },
          "13" : {
            "text" : "1-4",
            "actions" : {
              "_type" : "objectArray",
              "0" : {
                "action" : 3,
                "_type" : "object"
              }
            },
            "column" : 1,
            "color" : {
              "b" : {
                "_type" : "object"
              },
              "_type" : "objectArray",
              "g" : {
                "_type" : "object"
              },
              "a" : {
                "_type" : "object"
              },
              "r" : {
                "_type" : "object"
              }
            },
            "_type" : "object",
            "visibility" : "hidden",
            "page" : 0,
            "bgColor" : {
              "b" : {
                "_type" : "object"
              },
              "_type" : "objectArray",
              "g" : {
                "_type" : "object"
              },
              "a" : {
                "_type" : "object"
              },
              "r" : {
                "_type" : "object"
              }
            },
            "row" : 4,
            "id" : 13
          }
        },
        "_type" : "object"
      },
      "_type" : "objectArray"
    },
    "id" : "default"
  },
  "_type" : "objectArray",
  "app2" : {
    "_type" : "arrayObject",
    "name" : "app2",
    "_name" : "app2",
    "menus" : {
      "Top" : [ {
        "text" : "0-0"
      } ],
      "_type" : "objectArray"
    }
  }
}

无论如何,我已经成功使用了 emberFire 的一个分支,它从我的初始数组中提供了一个 emberFire 对象。(https://github.com/ember-meteor/emberFire/blob/refactor/emberfire-latest.js)

这行得通。

我有一个嵌入数组,我将它变成另一个 emberFire ObjectArray,并且

我的代码:https://github.com/jondthompson/menutest.

我使用与原始 ObjectArray 创建类似的行为为嵌套在“ssApp”内的“ssMenus”数组创建第二个,但它似乎没有填充来自 Firebase 的正确数据,甚至路由器也没有使用空数组触发。

正确的行为是,当您点击“应用”时,应用的名称出现在底部(有效!),菜单名称出现在它旁边(无效)。

无论如何,请帮忙!

【问题讨论】:

    标签: ember.js firebase


    【解决方案1】:

    我想通了。

    • 我正在手动编写子 Firebase 参考。我想出了如何通过原始的 firebase 对象来做到这一点。
    • 我没有将子引用封装在 firebase 对象中,而是试图将它注入到它不属于的地方。
    • 我正试图创建第二条路线,但我不需要它。

    我可能也错过了其他东西。我认为自己是一个初学者程序员,所以这是在扩展我的能力。

    我原来的 AppController..

    App.SsAppController = Ember.ObjectController.extend({
      needs: ["selected_ssApp", 'ssMenus'],
    
      selected: function(){
        return this.get('controllers.selected_ssApp.model') === this.get('model');
      }.property('controllers.selected_ssApp.model', 'model'),
    
      actions: {
        select: function(){
          var model = this.get('model');
          var menuList = this.get('controllers.ssMenus')
          Ember.debug("MenuList: "+ menuList);
          this.set('controllers.selected_ssApp.model', model);
          menuList.set('ref', new Firebase("https://menutest.firebaseio.com/jt-test/"+model.content._name+"/menus"));
    
        }
      }
    });
    

    我的新的和改进的(阅读:工作)MenuController..

    App.SsAppController = Ember.ObjectController.extend({
      needs: ["selected_ssApp", 'ssMenus'],
        menus: function(){
            var model = this.get('model'),
            ref = model.get('ref');
    
            return EmberFire.ObjectArray.create({ ref: ref.child('menus')});
        },
      selected: function(){
        return this.get('controllers.selected_ssApp.model') === this.get('model');
      }.property('controllers.selected_ssApp.model', 'model'),
      actions: {
        select: function(){
          var model = this.get('model'),
          menusObj = model.get('menus');
          this.set('controllers.selected_ssApp.model', model);
    
            this.set('controllers.ssMenus.content', this.menus());
        }
      }
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-01-28
      • 2013-09-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-08
      相关资源
      最近更新 更多