将Javascript数组传递到“post service”html

将Javascript数组传递到“post service”html,javascript,arrays,polymer,Javascript,Arrays,Polymer,我对Polymer还是个新手,通过修改Google提供的示例,我正在慢慢学习如何使用Polymer 但我遇到了一点问题 我有一个Javascript数组: var dash = [{"uid": 1, "text" : "It's a TOWN!", "username" : "Sam112", "avatar" : "../ToolKit/images/avatar-01.svg", "favorite": false, "num" : "Standard"}]; function addt

我对Polymer还是个新手,通过修改Google提供的示例,我正在慢慢学习如何使用Polymer

但我遇到了一点问题

我有一个Javascript数组:

var dash = [{"uid": 1,
"text" : "It's a TOWN!",
"username" : "Sam112",
"avatar" : "../ToolKit/images/avatar-01.svg",
"favorite": false,
"num" : "Standard"}];

function addto ()
{
    dash.push({"uid": 2,
        "text" : "It's a BABY!",
        "username" : "Pete223",
        "avatar" : "../ToolKit/images/avatar-01.svg",
        "favorite": false,
        "num" : "Standard"});
}
我想把这些数据放在“售后服务”模板文件中:

    <script>

    Polymer('post-service',
    {
        created: function()
        {
            this.posts = dash;
        },

        postsLoaded: function()
        {
            this.posts = this.$.ajax.response.slice(0);
        },

        setFavorite: function(uid, isFavorite)
        {
            console.log('Favorite changed: ' + uid + ", now: " + isFavorite);
        }
    });

</script>
所以我调用addto函数,然后加载index.html文件,我希望从数组加载数据

但是它加载了数组,但是从addto添加的对象没有加载

它显然是从原始数组加载的,而不是从addto函数加载的新条目


有没有办法让“售后服务”在阵列更改时加载阵列?再次感谢

我认为这不起作用,因为dash只分配给创建的处理程序中的this.posts,所以通常在聚合物元素的构造上。如果以后调用addTo,它将更改dash,但dash将不再分配给this.posts

也许可以将addTo函数添加到元素本身?比如:

谢谢kroonwijk

我最终放弃了,我只是使用本地存储和JSON的stringify和parse在聚合元素和Javascript数据之间进行传输

    localStorage["names"] = JSON.stringify(dash);
在聚合物方面

    Polymer('post-service',
    {
        created: function()
        {
            this.posts = JSON.parse(localStorage["names"]);
        },

        postsLoaded: function()
        {
            this.posts = this.$.ajax.response.slice(0);
        },

        setFavorite: function(uid, isFavorite)
        {
            console.log('Favorite changed: ' + uid + ", now: " + isFavorite);
        }
    });
更容易,抱歉,IE 7和更低

    Polymer('post-service',
    {
        created: function()
        {
            this.posts = JSON.parse(localStorage["names"]);
        },

        postsLoaded: function()
        {
            this.posts = this.$.ajax.response.slice(0);
        },

        setFavorite: function(uid, isFavorite)
        {
            console.log('Favorite changed: ' + uid + ", now: " + isFavorite);
        }
    });