Skip to content

Loading…

How to binding data from local json file. What i do wrong in this code? plz #2048

Closed
adisak1618 opened this Issue · 7 comments

4 participants

@adisak1618

i use jQuery to get persons.json from local file and set to this.employees. what should i do?

<link rel="import" href="../bower_components/polymer/polymer.html">



<script type="text/javascript" src="../bower_components/jquery-2.1.0.min/index.js"></script>


            <!--- render menu item ----->
              <dom-module id="employee-list">
                  <link rel="import" type="css" href="x-app.css">
                  <template>

                    <paper-menu class="list" on-iron-activate="_listTap">
                    <template is="dom-repeat" items="{{employees}}">


                        <paper-item>
                            <div> <span>{{item.nickname}}</span> </div>
                            <div> <span>{{item.phone}}</span> </div>
                        </paper-item>

                    </template>
                      </paper-menu>

                  </template>

                  <script>
                    Polymer({
                      is: 'employee-list',
                      ready: function() {


                            console.log(this.employees);
                         $.get('src/Persons.json',function(data){
                            this.employees = $.parseJSON(data).results;
                             console.log(this.employees);
                         });



                      }
                    });
                  </script>

                </dom-module>
              <!------- end render menu item ---------->
@adisak1618

json file look like this

{ "results": [
{
"address": "",
"createdAt": "2015-06-13T15:59:54.223Z",
"email": "",
"facebook": "dtac",
"fbID": "dtac",
"fname": "Dtac",
"instagram": "",
"lineID": "",
"listObjID": "CImULW8LTV",
"lname": "",
"nickname": "",
"objectId": "qHAjGpfqP6",
"phone": "1678",
"position": "Data Call Center",
"updatedAt": "2015-06-18T17:34:47.786Z"
}
]}

@adisak1618

i think, it because response time from jquery to get data.

@brian-cruickshank
@adisak1618

@brian-cruickshank Wowwww!! it work!!!

lot of thank :)

may be i need to read more, why can't use this in callback function

@brian-cruickshank
@LM450N

I think you can also use the JavaScript Function.bind instead of closure (I prefer this way).

Example:

$.get('src/Persons.json', function(data) {
    this.employees = $.parseJSON(data).results;
    console.log(this.employees);
}.bind(this));

And I think you can close the issue :wink:

@adisak1618

thank you, it's work also @kevinpschaaf

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.