0

I am running into the issue of getting Uncaught TypeError: Object # has no method 'get' when i try to display data in a template here are the different backbone parts:

Template:

<script type="text/template" id="class-template">


                <table class="table striped"></table>

                <thead>

                <tr>

                <th>Picture</th>
                <th>First Name</th>
                <th>Last Name</th>
                <th>Role</th>
                <th>Email</th>


                </tr>

                </thead>

                <tbody>

                <% _.each(users,function(user){ %>

                <tr>
                <td><%= user.get('picUrl') %></td>
                <td><%= user.get('firstName') %></td>
                <td><%= user.get('lastLame') %></td>
                <td><%= user.get('role') %></td>
                <td><%= user.get('email') %></td>




                </tr>

                <% }); %>

                </tbody>
                  </table>

    </script>

Data Models and Collection:

$.ajaxPrefilter(function(options, originalOptions, jqXHR) {

        options.url = 'http://localhost/' +options.url;

        });

        var Office = Backbone.Model.extend({

            defaults: {
                street : null,
                city : null,
                state : null, 
                country : null,
                postal_code : null,
            },
            initialize: function(){
                console.log("==> NEW LOCATION");

                // you can add event handlers here...


            }
        });
         var Person = Backbone.Model.extend({

            defaults: {
                picURL : null,
                firstName : null,
                lastName : null, 
                email : null,
                role : null,
                location : new Office()
            },
            initialize: function(){
                console.log("==> NEW PERSON MODEL");

                // you can add event handlers here...


            }
        });

        var Users = Backbone.Collection.extend({

        url:'loadData.php?list=16025,28477,28474,25513,16489,58911,04607',
        model:Person

        });

View:

var ShowClass = Backbone.View.extend({

            el: '.page',
            initialize: function() {
                _.bindAll(this); //Make all methods in this class have `this` bound to this class
            },

            template: _.template($('#class-template').html()),

            render: function() {

                var users = new Users();

                console.log('calling fetch');

                users.fetch();

                users.on("reset", function(users){
                    console.log('rendering with data:'+users.models[0].get('firstName'));
                    this.$el.html(this.template({users:users.models}));
                    console.log('finished');
                }, this);

            }

            });

I am able to see the data that is returned from the fetch call, so i know that I am getting data back. It all seems to fall apart when i send it to the template. Thanks in advance for all of your help!


Start by going through the templating section of the manual to be familiar with the design language. The rest of the manual is also very light reading, but not necessary for your needs.

Since you have some exposure to rails, you should know that unlike rail's default template engine, django's does not allow Python in the template. This is by design. There are lots of built-in filters and tags that provide most of the logic you would need. Developers can always write custom filters and tags, and some django applications ship with their own tags and filters. For your needs, you just need to know what the built-in ones are and how to apply them.

Unlike ruby, there is no strict concept of "asset pipeline" and built-in javascript libraries. Django's admin site (the CRUD front end) ships with jquery but its namespaced so you won't run into conflicts. Basically, in django - use whatever javascript/front end library tickles your fancy.

I found django's template language to be very easy to understand and you should not have any problems - especially with your familiarity with rails.

4

1 に答える 1

1

スクリプト テンプレートで get() を実行する代わりに、モデル全体を渡すのではなく、そのままの属性を渡す必要があります。

テンプレートも変更する必要があることは承知していますが、このようにテンプレートを抽象化し、テンプレート自体の外側でループを実行すると、エラーをより適切に処理できます。これにより、コードがモジュール化され、デバッグが容易になります。

意見:

users.on("reset", function(users){
    _.each(users, function (user) {
        var data = user.toJSON();
        this.$el.html(this.template({
                        picUrl: data.picUrl, 
                        firstName: data.firstName }));
    }, this);

テンプレートは次のようになります。

<td><%- picUrl %></td>
<td><%- firstName %></td>
...
于 2013-03-11T06:42:19.807 に答える