Backbone - Completely change model from View including it's reference - javascript

I would like to know if it is possible to completely change the model, not only it's values but it's actual reference on a View that´s already been initialized. I have seen this question but it only refers to model values.
I have created a reusable view (Which renders a Google Map) and it is used by other different views, not by creating the reusable module as a child view but navigating to a URL which shows a full screen map, which is an instance of my view.
This map View, receives a model on initialization which is later modified within this view, and since it is the same model reference it also updates the view's model that invoked (requested the navigation to) the map, I am invoking all views from my router and it is aware of which views are created and holds references to all of them so i can share models between view this way.
var myRouter= Backbone.Router.extend({
routes : {"viewA(/)" : "invokeViewA"
"viewA/map" : "invokeViewAMap"
//... same with ViewB
},
//...
invokeViewAMap : {
if (mapViewInstance){
//if the map already exists i want to update its model with the viewAinstance.model
} else{
//there is no map view yet so let's create it and send the model i need updated later on
mapViewInstance = new AddressFinderMapView({model : viewAInstance.model});
}
},
invokeViewBMap {
//similar to the above but using viewBInstance's model
}
});
var AddressFinderMapView = Backbone.View.extend({
//initialize function
//render function
//init google map
events : {"click button" : "updateModelWithAddress"},
updateModelWithAddress : function(){
//Here is where i need to update the model values, of the model that this view has received, so far it works with the model that was sent on initialization
this.model.set({
//latitude and longitude from the map
})
}
});
Additional thoughts:
I can stop reusing this Map View instance and create more instances, but that would defeat the purpose of calling Google Maps just once, at the end of the day, the map is used to select a location and return it to the view that invoked it.
I used to have an event being triggered from the Map View, so the other views would listen and update their own models, but since different views can live at the same time, it would update all models that were listening which is not what i wanted
I could send the current route on the trigger along with the latitud and longitude, and let each view filter whether it's their model that must be updated, but this feels more like a hack rather than an structured solution.

Just assign the new modal to the view instance's model property, like:
viewInstance.model = newModelInstance;

Related

View creation more than once time

I am new to Backbone. We have developed some POC.
We have one button on view. On every button click below function will be called and making server call to get the data.
Every time we are creating new view by passing collection to the view.
Is this a correct way to create view more than one at one time?
renderData : function(pageInfo, inputModel) {
MySpinner.start();
//Fetch the data
var deferedData = Application.request("app:generateData"inputModel);
$.when(deferedData).done(function(myCollection) {
var myView = new MyView({
collection : myCollection
});
//Display the view.
Application.mainViewRegion.show(myView);
Application.stop();
});
}
Have you considered using Marionette's CollectionView and resetting its collection using the response?
From the docs:
When the collection for the view is "reset", the view will call render on itself and re-render the entire collection.

GeoJSON layer does not get recreated when rendered using EmberJS's 'link-to' helper

I am trying to render a LeafletJS map where the colours of the states in the map are dependent on a global parameter that is set in the appropriate Ember route. The setting of the parameter is not the issue but rather the (re)creation of the geoJson layer. When hitting the URL for the first time or when reloading the page the correct map is created, however when the page is rendered using Ember's 'link-to' helper, the map still holds the state colours of the previous page.
drawAll: function() {
var that = this;
Ember.$.ajax('/data/sa_provinces.json').then( function(data){
Frontend.globalPaths = data;
that.get('store').findAll('province').then(function(provinces) {
provinces.forEach(function(province) {
var provinceGeoJSON = window.L.geoJson( province.get('dataFromJSON'),
{ style: province.get('geoJSONStyle'),
province: province,
onEachFeature: province.get('onEachFeature') });
province.set('geo_json', provinceGeoJSON);
provinceGeoJSON.addTo(Frontend.map);
window.province = province;
});
});
});
}.property('drawAll')
This drawAll function is located within a Ember controller and is called from an Ember template. The functions dataFromJSON, geoJSONStyle and onEachFeature are all called the first time a page is called or when the page is refreshed but not when the page is rendered using the Ember's 'link-to' helper. Neither are they called when the URL is entered manually.
If anyone has any ideas or experience with LeafletJS and/or Ember I would really appreciate your help. Thanks in advance, Greg.
The first issue I notice is that drawAll is a computed property, not a function - you seem to be confusing computed properties and functions.
http://emberjs.com/guides/object-model/computed-properties/
Ember computed properties are more like normal attributes that observe other variables, and recompute when those variables change. The property() method after the function declaration changes it into a computed property and specifies which variables the property depends on. On the last line you're specifying that drawAll observes itself, which doesn't make much sense.
You can't call functions from handlebars templates - you can only access properties. So you can access a property, with the side effect of causing that property's function to be called.
If you want just a function that is called as soon as the template loads, you can implement the didInsertElement function on that templates corresponding view, and the contents of the didInsertElement function will run when the template loads.
If you want a property that recomputes based on some conditions changing, you should change the last line to specify which conditions it is observing.
I can't be sure without more info about the template and controller you're using, but for your current use case it looks like you just want a function that runs whenever the template is inserted, so changing the drawAll to an actual function (by removing the .property('drawAll)) and calling it from didInsertElement of the corresponding view will rerun it every time the controller is inserted. Like:
didInsertElement: function() {
this.drawAll()
}
(You need to have created a view that corresponds to the controller in this context)

Should a Controller access a view's model directly?

I am in doubt with the usage of MVC in context to the example as follows :
Consider an example of a little drawing application.
Now say for example, there is a textbox where user can enter ANGLE for selected shape, Expected result should be, that each selected shape should get rotated as per the angle specified in textbox.
Say I have a shape object that is a View named as ShapeView
The given view has its own data like its position, current angle of rotation , stroke color, fill color, etc...
So I have a model for the shape as ShapeModel
So now I have Controller, that handles this textbox and the multiple shape views. A change in textbox value lets controller take necessary steps to rotate the shape.
QUESTION:
So the doubt is , should the controller directly access the shapeview's shapeModel and call the rotate method?
OR
should the Controller call shapeView's rotate method, that internally calls shapeModel's rotate method?
In short, should any outside entity access a view's model directly? Or should it go through View only? Is it a good idea to access model directly? Any issues or concerns if I access so.?
The view in Backbone is behaving as a controller.
E.g.
ShapeView = Backbone.View.extend
el: "input#angle"
events:
"onkeypress" : "update_angle"
update_angle: (ev)->
angle = $(ev.target).val()
#rotate(angle)
Most of time you don't need additional controller for the same view. The ShapeView controller can handle everything on its own. It's in charge of the views it's created for. If you need access other view controller or model, you can hook them on a global namespace, e.g. App = App || {}
If you have many values and those values needs to be accessed from other controller, you can create a Shape model, that model should not be kept within the View Controller, instead you should bind it to global namespace and set its attributes through global namespace.
You also can bind event on the model like this
shapeView = new ShapeView
shape = new Shape
shapeView.listenTo shape, "change:angle", shapeView.update_angle
This way, you can have multiple controller, listening to the same data model and get views updated accordingly.
Model and View controllers should be decoupled as much as you can.

Backbone model update not propagating to collection

I have a basic backbone collection of models.
The view I'm working within displays information about the model, allowing edits.
In the render of my view I capture the model based on a passed in 'id'.
render: function() {
this.model = myCollection.get(this.options.passedInId);
// do the render...
}
I then have a click event which updates the model and calls the render to re-render with the updates
updateModel: function() {
var me = this;
this.model.set('someFlag', true);
this.model.save(this.model.toJSON(), {
success: function(model, resp) {
me.render();
}
}
My problem is when it comes back through the render the second time the get from the collection returns a different instance of the model (I can see a different cId on it) which does not contain my changed "someFlag" property. Therefore my edits don't show up when the view is re-rendered. I know there might be a more effecient way of handling this but my question is why does this occur? Shouldn't the model fetched from the collection include the edits I made on that model?
Only other thing is the "myCollection" in this example may have been reset between the initial get and the next get after the edit, but the id is still present and it finds a model just one without any of the updates.
My issue was the collection was being reset between the render method and the updateModel method.
This causes the model to get out of sync with the collection to correct the problem all I needed to do was bind on the reset and make sure my model gets updated with the "new" version. I added this to my render.
var me = this;
this.collection.on('reset', function () {
me.model = this.get(me.model.id);
};

backbone.js - unbinding and swapping views

I'm going through the process of learning Backbone.js and I've come across a few things that look like they work... but behind the scenes, they might be causing some problems. One such issue is design patterns for swapping views.
I have a menu on the left (and corresponding view) that contains a list of users. Each user, when clicked, will display their corresponding list of movies to the right into another view. I do this by getting the model of the clicked user, building a new Movie collection based on that model, and rendering a new Movie View (to the right). If I click on another user, it does the same thing: it gets the user model, builds a new Movie collection, and renders a new Movie View to the right, replacing the DOM element. This looks fine -- but I'm worried about all of the new objects/bindings that are being created, and potential issues that could arise. So, what are my alternatives?
(1) Should I be trying to have the view redraw when the collection changes? How do I do this if I'm creating new collections?
(2) Or should I be unbinding everything when another user is clicked?
Here is my userlist view:
Window.UsersList = Backbone.View.extend({
el: $("#users"),
initialize: function(){
this.collection.bind('reset', this.render, this);
},
render: function(){
var users = [];
this.collection.each(function(user){
users.push(new UsersListItem({model: user}).render().el);
},this);
$(this.el).html(users);
return this;
}
});
In my UsersListItem view I capture the click event and call show_user_movies in my controller:
show_user_movies: function(usermodel){
// fetchMovies() lazy-loads and returns a new collections of movies
var movie_collection = usermodel.fetchMovies();
movie_list = new MovieList({collection: movie_collection, model: usermodel});
movie_list.render();
},
Thanks for any suggestions.
Just re-use the same MovieList view along with it's associated collection, using reset(models) to update the models in the collection, which should re-render the view. You can use the same pattern you have above with your MovieList view binding to the collection's reset event and re-rendering itself at that time. Note that usermodel.fetchMovies() doesn't follow the backbone asynchronous pattern of taking success/error callbacks, so I don't think the code works as is (maybe you simplified for the purpose of this question), but the point is when the new set of models arrives from the server, pass it to movie_list.collection.reset and you're good to go. This way you don't have to worry about unbinding events and creating new views.
show_user_movies: function(usermodel){
// fetchMovies() lazy-loads and returns a new collections of movies
movie_list.collection.reset(usermodel.fetchMovies().models);
},

Categories

Resources