Angularfire:如何通过一项属性访问一项? [英] Angularfire: How to access an item by one of it's properties?

查看:64
本文介绍了Angularfire:如何通过一项属性访问一项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Firebase数据的组织方式如下:

My Firebase data is organized this way:

+ myappname
  + customers
    + -JV2NQv3GmoM81zdUfTe
      + name: "Mary"
      + age: "24"
      + ...
    + -JV2N9NnItCfz5vB04RS
      + name: "John"
      + age: "32"
      + ...
    + ...
  + ...

如何用客户的名字找回客户?
名称保证是唯一的.
这是我的客户服务,当前:

How do I retrieve a customer by it's name?
The name is guaranteed to be unique.
This is my Customer service, currently:

app.factory('Customer', function ($firebase, FIREBASE_URL) {
  var ref = new Firebase(FIREBASE_URL + 'customers');
  var customers = $firebase(ref);

  var Customer = {
    all: customers,
    create: function (customer) {
      return customers.$add(customer).then(function (ref) {
        var customerId = ref.name();
        return customerId;
      });
    },
    set: function(customerId, customer) {
      return customers.$child(customerId).$set(customer);
    },
    find: function (customerId) {
      return customers.$child(customerId);
    },
    findByName: function (customerName) { // TODO...
    },
    delete: function (customerId) {
      var customer = Customer.find(customerId);
      customer.deleted = true;
      customer.$on('loaded', function () {
        customers.$child(customerId).$set(customer);
      });
    }
  };
  return Customer;
});

我应该在每次findByName()调用中扫描所有客户吗?
还是应该建立类似二级索引"的东西?
请提供一些建议,我才刚刚开始...:-(

Should I scan all the customers on each findByName() call?
Or should I build something like a "secondary index"?
Please, some advice, I'm just starting... :-(

推荐答案

多亏了Kato的表示,以及Frank van Puffelen的建议,我终于解决了自己的问题.
我确实在Firebase中添加了"index","customersByName"(记住磁盘空间很便宜,用户的时间不算是Firebase的座右铭……:-).
我没有按照所提到的答案中的指示进行操作,因为我认为此解决方案具有更广泛的用途:它可以针对多个索引"进行缩放...
我想在这里发布它,希望它可以对其他人有用.
我想看看评论:这种解决方案是否可能存在弊端?总体来说,对于某些用例,这是一个明智的解决方案吗?

Thanks to Kato indication, and Frank van Puffelen suggestions, I did at last solve my own problem.
I did add an "index", "customersByName", to my Firebase (remembering "Disk space is cheap, user's time is not" Firebase motto... :-).
I did not follow the direction in the referred answer, because I think this solution is of more general use: it scales for multiple "indexes"...
I want to post it here, hoping it can be of any use to other people.
I would like to see comments: does this solution have possible drawbacks? Is it an advisable solution, overall, for some use case?

app.factory('Customer', function ($firebase, FIREBASE_URL) {
  var ref = new Firebase(FIREBASE_URL + 'customers');
  var customers = $firebase(ref);
  var refByName = new Firebase(FIREBASE_URL + 'customersByName');
  var customersByName = $firebase(refByName);

  var Customer = {
    all: customers,
    create: function (customer) {
      return customers.$add(customer).then(function (ref) {
        var customerId = ref.name();
        customersByName.$child(customer.name).$set(customerId);
        return customerId;
      });
    },
    set: function(customerId, customer) {
      var oldname = customers.$child(customerId).name;
      if (customer.name !== oldname) {
        customersByName.$remove(oldname);
      }
      customersByName.$child(customer.name).$set(customerId);
      return customers.$child(customerId).$set(customer);
    },
    find: function (customerId) {
      return customers.$child(customerId);
    },
    findByName: function (customerName) {
      return customersByName.$child(customerName);
    },
    delete: function (customerId) {
      var customer = Customer.find(customerId);
      customer.deleted = true;
      customer.$on('loaded', function () {
        customersByName.$remove(customer.name);
        customers.$child(customerId).$set(customer);
      });
    }
  };

  return Customer;
});

这篇关于Angularfire:如何通过一项属性访问一项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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