如何检测Meteor中的反应元素何时更新

时间:2013-03-14 01:34:49

标签: meteor

我有一个标准模板,它使用一个集合中的反应元素列表来执行{{#each}}。

我遇到的问题是,我想在集合中更新特定元素时对其进行字体属性更改,只是为了引起注意它已被更新的事实。

我无法找到一个好的/优雅的方式来做到这一点。有人能指出我正确的方向吗?谢谢!

2 个答案:

答案 0 :(得分:2)

您可能需要检查光标观察,当某些文档更新或更改时,您可以执行任何操作,如果事件被调用。

li#model-1273927381273 model-1273927381273

Modal.find().observe
    changed: (newDocument, oldDocument) ->
        ($ '#model-'+ newDocument._id).action()

http://docs.meteor.com/#observe

i use similar code currently but dont know how to prevent init documents load.

答案 1 :(得分:0)

与您的问题类似,我想要' flash'每当特定会话变量(GPS位置)改变时,div。我的解决方案如下。

在/client/main.js中:

Session.setDefault('lat', 0);
Session.setDefault('lng', 0);
Session.setDefault('locationChange', 1);

Meteor.setInterval(function() {
    getLocation();

}, 5000); 

function getLocation() {
    Location.locate(function(pos){
        var oldLat = Session.get("lat");
        var oldLng = Session.get("lng");

        Session.set('lat', pos.latitude);
        Session.set('lng', pos.longitude);

        if (oldLat != Session.get("lat") || oldLng != Session.get("lng"))
            Session.set("locationChange", -1 * Session.get("locationChange"));

    }, function(err){
        console.log("Location not obtained: ", err);
    });     
}

在模板中,例如/client/templates/someTemplate.html,我想提醒用户注意更改:

<span class="glyphicon glyphicon-screenshot" aria-hidden="true" data-locationChange='{{locationChange}}'></span>

在关联的javascript文件中,例如/client/templates/someTemplate.js:

Template.appBody.helpers({
    locationChange: function() {
        $("#locationIcon").fadeOut(100).fadeIn(100);

        return Session.get("locationChange");
    },    
});

所以,现在,每当GPS坐标发生变化时(不一定在它们被刷新时),&#39; locationChange&#39;从1翻转到-1,反之亦然。这会触发&#39; locationChange&#39;帮助器,它更新(无意义的)属性&data-locationChange&#39;,但也会触发一些闪烁模板中跨度的jQuery脚本。