I am working with backbone.js v 0.5.3.
I am wondering if there is a better way to set the id property of a view element. It seems redundant to always have to set it like so:
view = new Views.Foo( model: #model, id: 'foo-' + #model.id )
It would be nice if I could define the id in the view class like so
class Views.Foo extends Backbone.View
id: 'foo-' + #model.id
It is kind of nitpicky but I find myself defining id and data-attributes way too often.
Has anyone come up with a good way to initialize view el attributes?
You can put it to the constructor:
class Views.Foo extends Backbone.View
initialize: (options)->
#id = 'foo-' + #model.id
$(#el).attr 'id', #id
Related
I feel like I'm missing something crucial in rest framework that will help me understand this but so far browsing the documentation has not enlightened me.
I have a model Circuit that can have multiple Component models linked to it, this many to many relationship goes through a CicuitHasComponent table. It can also have sub-circuits, so a many to many relationship with itself through CircuitHasCircuit table. I want to show a detail view for the Circuit (using angular JS on the front end) that lists all the Components and sub-Circuits, and allows me to build new sub-object resources in the angular JS and post them. So I have the following serializers
class ComponentSerializer(HyperlinkedModelSerializer):
class Meta:
model = Component
fields = (
'id', 'name'
)
class CircuitHasComponentSerializer(HyperlinkedModelSerializer):
class Meta:
model = CircuitHasComponent
fields = (
'circuit',
'component'
)
class CircuitHasCircuitSerializer(HyperlinkedModelSerializer):
class Meta:
model = CircuitHasCircuit
fields = (
'circuit',
'subcircuit'
)
class CircuitSerializer(HyperlinkedModelSerializer):
components = CircuitHasComponentSerializer(many=True)
subcircuits = CircuitHasCircuitSerializer(many=True)
class Meta:
model = Circuit
fields = (
'id',
'name',
'components',
'subcircuits'
)
I have corresponding ModelViewSets for each serialiser, but I am not sure on the neatest way to GET a specific subset of CircuitHasComponent (f.e.) objects for a given Circuit ID. Nor am I sure how to POST to the CircuitHasComponent view using JS models of the corresponding Circuit and Component.
Should I be using a nested method for this? or should things be broken down further?
When i am creating the model by extending "Ext.data.Model" class, getter/setter methods are behaving differently than default .get and .set methods available from data.Model
It seems like one can either use getter/setter methods or .get/.set methods, because they seem to be maintaining separate set of fields.
Why is it so? Parden me if question looks silly, i am learning Ext JS an trying to understand how it works. I am using library version ExtJS4.2.1
Class
Ext.define("Ext.model.Invoice", {
extend : "Ext.data.Model",
fields : [{name : 'id'}, {name : 'taxId'}, {name : 'name'}],
config : {
name : 'Tejas',
taxId : '23746'
},
constructor : function(config) {
this.callParent(arguments);
this.initConfig(config);
}
});
HTML
Ext.onReady(function() {
var invoice = Ext.create("Ext.model.Invoice");
console.log("Before, invoice.get('name'):", invoice.get('name'));
console.log("Before, invoice.getName():", invoice.getName());
//Modifying name
invoice.setName("Mr. Smith");
invoice.set("name","Mr. Tony");
console.log("Updating names using setName and set('name')");
console.log("After, invoice.get('name'):", invoice.get('name'));
console.log("After, invoice.getName():", invoice.getName());
});
OUTPUT
Before, invoice.get('name'):
Before, invoice.getName(): Tejas
Updating names using setName and set('name')
After, invoice.get('name'): Mr. Tony
After, invoice.getName(): Mr. Smith
With config configuration property you are defining list of configuration options with their default values, but not default model data.
When instance of object is creating, for each property defined in config is automatically created setter and getter method, and object property with same name as config property.
Ext.data.Model stores model data in its private data property. For example you can try to dump model data for name field by:
console.log(invoice.data.name);
So by setter and getter you access object property, but by model.get() and model.set() you access model's data stored in model's private data property.
The following would be the model of a simple backbone application. So consider i want to have more than one url calls, i am told i should be using collections(definition is a collection of models).
Model format :
var Books1 = Backbone.Model.extend({
urlRoot: '/books1'
});
var Books2 = Backbone.Model.extend({
urlRoot: '/books2'
});
Collection format :
var Books = Backbone.Collection.extend({
url: '/books'
});
My question is how can i like combine more than one model in a collection ?
To be more clear :
Consider i have currently 8-10 models in a single view(need to call 8+ server requests from a single view). I am succesfully doing this via models. But from my initial research i came to conclusion that i should be using collection which will be a collection of all the models used. So by passing the collection to view, i will be able to call out different models at different part of view as requried.
And how do i use it in a view?
To be more clear :
for a model now i use this.model.save() or newmodel.save() after declaring the model before the place where i need to have the request done.
Cheers
lots of properties is handled via http://underscorejs.org/#result so you can pass urlRoot as function
var Books = Backbone.Model.extend({
urlRoot: function(){
return condition ? '/books' : '/bum'
}
});
same in collection. But I don't think so, its a good idea to combine 2 or more models in one collection. Just make a new collection for another model, if its possible....
what about view?
you can pass collection to view instance like this:
var SomeView = Backbone.View.extend({
initialize: function(opt){
this.collection = new opt.collection
}
});
var someView = new SomeView({collection: Books})
I'm using django+tastypie+backbone.js with backbone-relational.
Let's say i have model(coffee script):
class Track extends Backbone.RelationalModel
And somehow i get the first object's URI:
api/track/1
Result in JSON have to be something like:
{
'title': 'Mytrack',
'length': '120'
}
How can i get full model JSON with all attributes using this URI?
You need to set the model's urlRoot (/api/track), then create a new model with the id you want (1), and call .fetch on the model. The fetch call will be asynchronous, so you need to wait for the success callback before you can access the full properties:
class Track extends Backbone.RelationalModel
urlRoot:"/api/track"
track = new Track id:1
track.fetch
success:(model) -> console.log model
The number of models has grown quickly in my application. I'm wondering about your standard practices regarding backbone.js. Lets say you want to create a view that requires 2 other models. Do you create a new model class to contain the 2 models like this:
var m = new TheModel({
model1: new Model1,
model2: new Model2
});
var view = new TheView({model:m});
or do you just do something like:
var m = {
model1: new Model1,
model2: new Model2
};
var view = new TheView({model:m});
The second seems better because then I don't need the extra model class TheModel. But if I mix the two methods, then in my view I have to remember which style I'm using because if I want to get access to model1 or model2 then there are two different ways:
var m1 = this.model.get('model1');
or this way in the second scheme:
var m1 = this.model.model1;
Which is better in your opinion?? How do you organize all the models and views?
Thanks!
Unless there is a reason to link your models, I would not create a new model that aggregates other models. The second option is better because you are not linking them.
I prefer to separate them even further:
var view = new TheView({purchases: purchasesModel, user: userModel });
Then, inside the view, instead of referencing this.model, you reference this.purchases and this.user specifically.
You see, the model property on the views is really just a convention. There is nothing special about the model property in the view except that the model property will get copied automatically in the constructor. Other than that, there is no reference to the model in Backbone.View.
Of course, this means that you need to do something with it:
var TheView = Backbone.View.extend({
initialize: function(options) {
this.purchases = options.purchases;
this.user = options.user;
}
});
This way, you are being explicit about the multiple models that you require. I like this better than verison #2 because you are not as explicit about the requirements of the View.
You should use Collections in this case. Here you can read more about Collections: http://backbonetutorials.com/what-is-a-collection/