I am new to backbone and trying out my first app after going through a few tutorial apps.
I was wondering what is the best way to accomplish the following
On the backend(rails)
I have a model name Business, It is a complex model with a lot of attributes, It has an associated address (has_one :address) and has an avatar and another profile pitcure and lot more.
From my frontend I want to be able to fetch and update specific parts of the business profile, Lets say I only want to fetch the basic_info which includes name, category and address than I want to be able to update the profile picture and the avatar.
What I have seen in backbone is that the model has the methods save, update, fetch, destroy
What if I want to have other methods like fetch_basic_info, fetch_profile_picture, update_profile_picture ? and against these I want the associated views to be notified accordingly.
Here is what I have come up with
Lets say I want to fetch basic info
-
add a function
fetch_basci_infoto the backbone model- inside this function send an custom ajax request using
$.ajaxto the server - manually trigger the event
"basicinfo:fetched"
- inside this function send an custom ajax request using
-
inside my router function
- create model object
- create a new view lets say
BasicInfoViewand pass it the model object - inside the view bind an even of the model lets say
model.bind('basicinfo:fetched', this.render) - when the router is initialized call
model.fetch_basic_info(in the router init)
So the router is called it creates view binds a custom event and calls model.fetch_basic_info() request is sent to the server response is returned ( do I call set manually to set the attributes of the backbone model here). After that the custom event event is triggered the view is notified and it renders itself
This is my first real backbone application so if I am doing something really out of the way stop me.
What are your thoughts on this.
Thank you for reading and feedbcak.
What you’re trying to do isn’t very RESTful. If you’re trying to do this to save resources or network bandwidth then its almost certainly a premature optimization – unless we are talking about hundreds or thousands of fields – in which case there’s a better solution.
The truth is, Fetching just a profile picture uses just about the same resources as fetching 50-100 fields. Yes slightly more data, but considering 90% of the work, latency, resources and waiting time in a network connection comes from establishing the connection you’re not actually saving that much.
Plus on the database end, a
select * from businesses where id=123uses only a tiny bit more than
select profilepic from businesses where id=123since the hardest part of the job is establishing a connection to the database and finding the correct row. After that, its just a bit more data – adding 50 extra columns will have an unnoticeable effect on performance.
The only time this falls over is if your model / table contains hundreds or thousands of attributes. In this case the solution is to split your model up into sub-models. And deal with them individually via REST. But they should be Business logic types. For example Business contains Address, Employee, ShareStructure.
I myself used to be a premature optimizer… “Must not return 10 columns when I only need 1 column”. But if try try to write web service APIs for every subset of data in its varying combinations you might ever need, your API will be practically unusable and unmaintainable. You’ll also never get any work done.
Say you wanted to get CocaCola’s profile picture off facebook’s API, you would just call:
https://graph.facebook.com/cocacola
and get the Picture property. Who cares if you don’t need the rest of the data? It keeps things simple, restful and easy to maintain.