2013-07-26 29 views
2

我尝试新的数据推入一个jQuery函数调用filter.js(https://github.com/jiren/filter.js将内容推送到fliter.js(jQuery的过滤脚本)

问题是,我无法弄清楚如何数组addData应该看起来像。

下面是脚本演示: http://jiren.github.io/filter.js/stream.html

这里就是它从开始获取JSON: /examples/data/top_movies_data.json

关于这个功能的唯一文件说:

Add more data to existing filter

If you are streaming json data using ajax then you can add data like this

fJS.addData(data)

Here fJS is Filter.js object and data is json records.

我正在尝试使用此代码制作一个新框,它回显的“添加”,但没有框被添加,它不打印“添加”

<button id="add">ADD</button> 
<script> 
$("#add").click(function() { 
    alert("add"); 
    var myNewBox = '[{"name":"Woho","outline":"Two imprisoned men bond over a number of years, finding solace and eventual redemption through acts of common decency.","rating":"9.3","director":"Frank Darabont","year":"1994","stars":["Tim Robbins","Morgan Freeman","Bob Gunton"],"runtime":"142","genre":["Crime","Drama"],"certificate":"R"}]'; 
    fJS.addData(myNewBox); 
    alert("added"); 
}); 
</script> 

控制台给我这个错误:的ReferenceError:FJS没有定义

我会很高兴,如果有人可以帮助我,我应该如何改变我的功能,可以添加数据。

非常感谢提前!

+1

我不明白你在哪里定义'fJS'? – Elen

+0

我相信它是在/ example /文件夹中的filter.js中定义的:https://github.com/jiren/filter.js/blob/master/examples/filterjs.js –

+0

不,它是函数。但你必须把它分配给一个变量或元素。文档是相当差的寿...它被定义为'var fJS = filterInit('mustache');' – Elen

回答

0

可能发生的冲突: 当usinf第三方插件莫名其妙$不是在其固有的jQuery状态。因此,要确保我把它作为其原生jQuery的,它必须被传递给函数,像这样......

jQuery(function ($) {...}); 

这样做,你告诉jQuery来实例化和职能范围内重新分配。

那么下一步将是把你的代码里......

jQuery(function ($) { 
    $("#add").click(function() { 
     alert("add"); 
     var myNewBox = '[{"name......]'; 
     fJS.addData(myNewBox); 
     alert("added"); 
    }); 
}); 

此外,我会建议你转,你必须把myNewBoxaddData,你必须把addDatamyNewBox,这更有意义,因为你想你推addData,像这样...

var addData= '[{"name......]'; 
    fJS.addData(myNewBox);