javascript - Backbone get() returns undefined -
problem lies in postrouter var in app.js file. logging this.posts.tojson() returns data in db calling passing id returns undefined. routing works id passed in url , logged in function. using mongo store data , monk query db.
my server file
//configure app.configure(function(){ app.use(express.json()); //parses json requests being sent server app.use(express.static( path.join(__dirname, 'public') )); //serve files path given }); app.get("/posts", function(req, res){ var db = req.db; db.get("postscollection").find({}, function (err, results) { res.json(results); }); }); //create route using express rest api app.get('/*', function(req, res){ var db = req.db; db.get("postscollection").find({}, function (err, results) { res.render("index.ejs", { posts: json.stringify(results) }); //pass object of values use in template }); }); app.listen(3000);
my app file
//post var post = backbone.model.extend({}); //posts var posts = backbone.collection.extend({ model: post, url: "/posts" }); //postlistview var postlistview = backbone.view.extend({ tagname: 'li', template: _.template("<a href='/posts/{{_id}}'>{{title}}</a>"), render: function () { this.el.innerhtml = this.template(this.model.tojson()); //this.model view instance return this; }, events: { 'click a': 'handleclick' }, handleclick: function (e) { e.preventdefault(); postrouter.navigate($(e.currenttarget).attr("href"), { trigger: true }); //navigate specified route , trigger routing } }); //postslistview var postslistview = backbone.view.extend({ template: _.template("<h1>my blog</h1><ul></ul>"), render: function () { this.el.innerhtml = this.template(); var ul = this.$el.find("ul"); //this.$el == $(el) this.collection.foreach(function (post) { ul.append(new postlistview({ model: post }).render().el); }); return this; } }); //postview var postview = backbone.view.extend({ events: { 'click a': 'handleclick' }, initialize: function(){ _.bindall(this, "render"); this.template = _.template($("#postview").html()); //to prevent template loading before scripts }, render: function () { var model = this.model.tojson(); this.el.innerhtml = this.template(model); return this; }, handleclick: function (e) { e.preventdefault(); postrouter.navigate($(e.currenttarget).attr("href"), {trigger: true}); } }); //postrouter var postrouter = backbone.router.extend({ initialize: function (options) { this.posts = options.posts; //the posts collection this.main = options.main; //the main id div in view }, routes: { '' : 'index', 'posts/:id': 'singlepost' }, index: function () { var pv = new postslistview({ collection: this.posts }); this.main.html(pv.render().el); }, singlepost: function (id) { console.log("view post " + id); //logs id passed console.log(this.posts.tojson()); //logs data in db var post = this.posts.get(id); //returns undefined var pv = new postview({ model: post }); this.main.html(pv.render().el); } });
the index works fine , display data trying view individual posts breaks application.
Comments
Post a Comment