如果我调整使用的数据,我怎样才能使火力地堡自动更新呢? [英] If I restructure data for use, how can I make Firebase automatically update it?

查看:133
本文介绍了如果我调整使用的数据,我怎样才能使火力地堡自动更新呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我使用:

  REF VAR =新火力地堡(https://myURL.firebaseio.com/);
VAR同步= $火力点(REF);
。VAR firebaseData = $同步asObject();

firebaseData 会从 https://myURL.firebaseio.com/ 变化的数据变化。他们将是同步的。

如果我重组的数据,并将其定义为一些新的变量 VAR newStructure ,我怎么能得到它与 HTTPS同步:// myURL .firebaseio.com / firebaseData

有没有一种方法,我可以看在 firebaseData 的变化和调结构调整的方法?

有没有办法知道在 firebaseData 专变,并只对 newStructure

有没有一种方式, newStructure 可与火力地堡直接同步,尽管有不同的结构解析的数据?


解决方案

您变量 firebaseData 重新presents您的收藏火力

所有改变您对 firebaseData 将在连接上syncronized:

  REF VAR =新火力地堡(https://myURL.firebaseio.com/);
VAR同步= $火力点(REF);
。VAR firebaseData = $同步asObject();

您可以观看,看看是否有任何改变对该对象进行:

  firebaseData。$表(函数(事件){
    的console.log(更改这个火力点对象所做的);
    //然后就可以调用它可以调整你的数据的功能:
    restructureData(firebaseData,事件);
});

所以你的 restructureData 函数可能看起来像这样:

  VAR restructureData =功能(firebaseObj,事件){
    //这个函数更新的每一个变化,以火力火力
    //但是我们不希望运行该功能后,再次更新
    如果(event.key ===changesMadeToFirebase)返回;
    firebaseObj.changesMadeToFirebase + = 1;
    firebaseObj。$保存()。然后(函数(){
        的console.log(数据重组);
    },功能(错误){
        的console.log(有一个错误:ERR);
    });
};

这允许你检查有多少已经作了修改,以火力点,除了改变字段changesMadeToFirebase

虽然这是一个很小的例子,你就可以在这里找到更多的:

https://www.firebase.com/docs/web/库/角/ api.html

If I use:

var ref = new Firebase("https://myURL.firebaseio.com/");                
var sync = $firebase(ref);                                
var firebaseData= sync.$asObject();

firebaseData will change as data from https://myURL.firebaseio.com/ changes. They will be in sync.

If I restructure data, and define it as some new variable var newStructure, how can I get it to sync with https://myURL.firebaseio.com/ or firebaseData?

Is there a way I can watch for changes in firebaseData and recall the restructuring method?

Is there a way to know what specifically changed in firebaseData and make only relevant changes to newStructure?

Is there a way that newStructure can sync directly with Firebase, albeit have data parsed with a different structure?

解决方案

Your variable firebaseData represents your firebase collection.

All changes you make to firebaseData will be syncronized across connections:

var ref = new Firebase("https://myURL.firebaseio.com/");                
var sync = $firebase(ref);                                
var firebaseData = sync.$asObject();

You can watch to see if any changes have been made to the object:

firebaseData.$watch(function(event){
    console.log("Change made to this firebase object");
    // Then you can call a function which could restructure your data:
    restructureData(firebaseData, event);
});

So your restructureData function could look like this:

var restructureData = function(firebaseObj, event){
    // This function updates firebase on every change to firebase
    // But we don't want to update it again after running this function
    if(event.key === "changesMadeToFirebase") return;
    firebaseObj.changesMadeToFirebase += 1;
    firebaseObj.$save().then(function(){
        console.log("data restructured");
    }, function(err){
        console.log("There was an error:", err);
    });
};

This allows you to check how many changes have been made to firebase, except for the changes to the field "changesMadeToFirebase"

Although this is a very small example, you will be able to find much more here:

https://www.firebase.com/docs/web/libraries/angular/api.html

这篇关于如果我调整使用的数据,我怎样才能使火力地堡自动更新呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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