backbone.js

Backbone.js not rendering

人盡茶涼 提交于 2020-01-15 12:02:53
问题 My collection is not rendering for some reason. Cannot find out why. TreeItem = Backbone.Model.extend({ }); TreeList = Backbone.Collection.extend({ model: TreeItem, url: "/get_tree_list" }); window.tree_list = new TreeList(); // VIEW window.TreeItemView = Backbone.View.extend({ tagName: 'li', initialize: function(){ _.bindAll(this, 'render'); }, render: function(){ $(this.el).html('<span>'+this.model.get('title')+'</span>'); return this; } }); window.TreeListView = Backbone.View.extend({ el:

Backbone.js not rendering

二次信任 提交于 2020-01-15 12:02:08
问题 My collection is not rendering for some reason. Cannot find out why. TreeItem = Backbone.Model.extend({ }); TreeList = Backbone.Collection.extend({ model: TreeItem, url: "/get_tree_list" }); window.tree_list = new TreeList(); // VIEW window.TreeItemView = Backbone.View.extend({ tagName: 'li', initialize: function(){ _.bindAll(this, 'render'); }, render: function(){ $(this.el).html('<span>'+this.model.get('title')+'</span>'); return this; } }); window.TreeListView = Backbone.View.extend({ el:

Backbone.js toJSON() not including attributes

折月煮酒 提交于 2020-01-15 11:44:07
问题 I really can't wrap my head around this one. For some reason, (and I suspect it has something to do with asynchronous requests) my model attribute is not being converted properly using the toJSON() function. FWIW, I'm attempting to do lazy loading with django-relational fetchRelated(). Here's where I made the fetchRelated() request and create a new view. $.when(usermodel.fetchRelated("movies")).then(function(){ var newview = new UserMovieList({model: usermodel}); newview.render(); }); And

Skip validation of model attribute using Backbone.Validation

有些话、适合烂在心里 提交于 2020-01-15 10:07:46
问题 I have a view that is rendered dynamically. It may have some inputs or may not have. After a user fills everything and tries to send data I call this.model.isValid(true) ( or this.model.isValid() ) and it returns false even if the data from inputs is valid. I think the cause is Backbone Validation tries to validate attributes of inputs we did not render. Is there any solution to skip model attributes if we have no sticked elements of a view? UPDATE: My model is similar to this: MyApp.module(

Spy on Backbone.js View Functions with Jasmine

青春壹個敷衍的年華 提交于 2020-01-15 05:48:05
问题 I'm trying to write a Jasmine spec to verify that a view's function is called when a model is added to the view's collection. In the view's initialize function I do this.collection.on('add', this.fooAdded, this); In my Jasmine spec I'm doing: describe('Foo View', function() { it('should call fooAdded when a Foo is added', function() { var view = new FooView({collection: new FooCollection()}); spyOn(view, 'fooAdded').andCallThrough(); view.delegateEvents(); view.collection.add({name: 'foo'});

How to populate a Backbone.js collection's _byId array so that I can use `get` on it?

泄露秘密 提交于 2020-01-15 05:24:37
问题 I have a collection, and the collection.models returns an array of models. However, when I call collection.get(someId) (and this id is the id of the model that is in the collection.models array), I get undefined . Looking at collection._byId , it looks like an empty object. How do I properly populate _byId , so that I can use get ? Or perhaps I'm doing something wrong when initializing my collection, which is why _byId is empty. 回答1: I'm a little late, but hopefully this is still useful to

backbone.js adding model to collection adds to all models in collection

随声附和 提交于 2020-01-14 12:12:30
问题 Playing around with backbone.js, so far I've created models and collections for a deck of cards and two players. The problem is when I try move a card from the deck and add it to the player's hand the card gets added to all players hands. Here's my offending code, hopefully a boffin will spot the mistake straight away: //the first card goes to player1 var topCard = deck.at(0); //A of hearts deck.remove(topCard); var hand = players.at(0).get("hand"); hand.add(topCard); //the second card goes

Backbone collection sortBy

会有一股神秘感。 提交于 2020-01-14 10:40:08
问题 I make my first backbone app and get some problems with collection sorting. After using this var SortedFriends = MyFriends.sortBy(function(friend) { return friend.get("uid"); }); console.log(SortedFriends) show that SortedFriends contains sorted models, but when i try to use collection functions like 'SortedFriends.each' or 'SortedFriends.at' it make error: TypeError: SortedFriends.each is not a function. Code: var Friend = Backbone.Model.extend({}); var Friends = Backbone.Collection.extend({

Why doesn't Backbone Collection fetch return a promise

不想你离开。 提交于 2020-01-14 10:18:08
问题 The following example code works well: Auth_controller.prototype.isLogged = function(){ //Check if the user is authenticated var getAuthStatus = this.auth_model.fetch(); return getAuthStatus; }; Auth_controller.prototype.redirect = function(fragment, args, next){ var getAuthStatus = this.isLogged(); var self = this; $.when(getAuthStatus).then(function(response){ //Do something with the response } }); This doesn't seem to work for a Collection though. When I console log the collection, I get

Lazy load items with filtering

℡╲_俬逩灬. 提交于 2020-01-14 09:33:03
问题 I am using Backbone.js to load 20 items at a time on the page, getting more items when you scroll down to the bottom until there are none left to fetch from the server. At the same time, I want an input field up top that as you type a name, it filters the items that match. The issue is, if you haven't scrolled to the bottom yet and fetched the full set, the input filter will only match the items that are currently on the page. What is the best solution technically and visually for combining