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