2

我正在构建一个网页以显示基于分析的应用程序的所有用户最近的捐赠。我从这里找到的示例Todo应用程序构建此应用程序:https://parse.com/tutorials/todo-app-with-javascript。这里是我的主.js文件的代码(这主要是反映了教程todo.js比替换类名和其他等):完整的分析对象不可用于下划线模板

//Donation Model 
//-------------- 
var Donation = Parse.Object.extend("Donation", { 
    //instance methods 
    //Default attributes 
    defaults: { 
    }, 

    //Ensure that each donation created has content 
    initialize: function() { 
    } 
}); 


// This is the transient application state, not persisted on Parse 
var AppState = Parse.Object.extend("AppState", { 
    defaults: { 
     filter: "all" 
    } 
}); 

//Donation Collection 
//------------------- 

var DonationList = Parse.Collection.extend({ 
    model: Donation 

}); 

//Donation Item View 
//----------------- 

var DonationView = Parse.View.extend({ 
    tagName: "tr", 
    template: _.template($('#donation-template').html()), 

    //The DOM events specific to donation 
    events: { 

    }, 

    initialize: function() { 
     _.bindAll(this, 'render'); 
     this.model.bind('change', this.render); 
    }, 

    render: function() { 
     $(this.el).html(this.template(this.model.toJSON())); 
     return this; 
    } 

}); 


//The Application 
//--------------- 

var AdminView = Parse.View.extend({ 
    el: ".content", 
    initialize: function() { 
     var self = this; 
     _.bindAll(this, 'addOne', 'addAll', 'render'); 
     this.$el.html(_.template($('#admin-template').html())); 

     ////create out collection of donations 
     this.donations = new DonationList; 

     //setup the Parse query 
     var query = new Parse.Query(Donation); 
     query.include("user"); 
     query.include("charity"); 
     query.include("business"); 
     this.donations.query = query; 
     this.donations.bind('add', this.addOne); 
     this.donations.bind('reset', this.addAll); 
     this.donations.bind('all', this.render); 

     this.donations.fetch({ 
      success: function(donations) { 
       for(var i = 0; i < donations.length;i++) { 
        console.warn(donations.models[i]); 
       } 
      } 
     }); 
     state.on("change", this.filter, this); 
    }, 

    render: function() { 
     this.delegateEvents(); 
    }, 
    filter: function() { 
     this.addAll(); 
    }, 
    addOne: function(donation) { 
     var view = new DonationView({model: donation}); 
     this.$("#donation-list").append(view.render().el); 
    }, 
    addAll: function(collection, filter) { 
     this.$('#donation-list').html(""); 
     this.donations.each(this.addOne); 
    } 
}); 


//The main view for the app 
var AppView = Parse.View.extend({ 
    // Instead of generating a new element, bind to the existing skeleton of 
    //the App already present in the HTML. 
    el: $("#adminapp"), 

    initialize: function() { 
     this.render(); 
    }, 

    render: function() { 
     new AdminView(); 
    } 
}); 

var state = new AppState; 
new AppView; 

当模特被取出从parse在

this.donations.fetch({ 
      success: function(donations) { 
       for(var i = 0; i < donations.length;i++) { 
        console.warn(donations.models[i]); 
       } 
      } 
     }); 

我完全掌握了捐赠中的所有指针关系,例如用户及其所有属性,慈善机构及其所有属性等。但是,我有兴趣显示捐赠用户的用户名下划线模板,但在此时,捐赠用户对象不再具有用户名属性。就好像某些属性在查询返回时和为新模板提供新捐款集之间被删除一样。这里有下划线的模板:

<script type="text/template" id="donation-template"> 
    <td><%= donationAmount %></td> 
    <td><%= charity %></td> 
    <td><%= user %></td> 
    <td><%= business %></td> 
    <td><%= createdAt %></td> 
</script> 

donationAmount和createdAt按预期显示,但慈善机构,用户和业务都只是显示为[Object对象],我不能用点号访问他们的任何属性。我怎样才能确保我需要的所有这些指针关系的属性都可以被下划线视图使用?

+0

阅读解析JS SDK文档如何填充指针和关系。他们不是默认填充 – charlietfl 2015-01-20 18:33:02

+0

不包括应该完成这个?如果我将它们记录在查询的成功块中,我可以看到所有的对象属性,但之后它们似乎不可用。 – mike 2015-01-20 18:42:49

回答

0

设置的UserList的作为模型集合

return Parse.Collection.extend({ 
     model: User 
}); 

做“关系”的查询和设置局部模型的/ var

this.collection = new UserList(); 
    this.model = this.options.model; 

    var relation = this.model.relation("users"); 
    this.collection.query = relation.query();  
    var user = this.model.id; 
    var username = this.model.get("name"); 
    this.collection.fetch(); 
+0

对不起,我不知道你在暗示我在哪里放置这段代码。你能详细说明一下吗?谢谢你的帮助 – mike 2015-01-21 14:56:31

+0

。示例代码来自backbone/marionette/project访问parse.Users中的关系查询(通过关系或指向用户的查询访问)。如果你在集合中使用MVC框架,我认为它可能会提供一些想法。 – 2015-01-21 15:54:07

+0

谢谢!我会看看示例代码。 – mike 2015-01-21 16:55:09