require. how to pass a model to a view (backbone) - javascript

I'm using require.js with backbone. My question: is how do I fetch.() my model from within my view. What I have tried is below, however I get the error 'Campaign is undefined'. I think I'm very close:
Model:
define([
'underscore',
'backbone'
], function(_, Backbone) {
var Campagin = Backbone.Model.extend({
urlRoot: '/api/v1/campaign/'
});
return Campagin;
});
View:
define([
'jquery',
'underscore',
'backbone',
'views/RewardView',
'views/FriendRewardView',
'models/CampaginModel',
'text!templates/backbone/portal/campaignTemplate.html'
], function($, _, Backbone, campaignTemplate){
var CampaginView = Backbone.View.extend({
el: '#campaign-panel',
render: function(options) {
if(options.id){
var campaign = new Campagin({id: options.id});
campaign.fetch({
success: function(campaign){
// We can only get the reward when the campaign reward url is returned.
var rewardview = new RewardView();
rewardview.render({reward_url: campaign.get('participant_reward')});
var friendview = new FriendRewardView();
friendview.render({reward_url: campaign.get('friend_reward')});
var template = _.template(campaignTemplate, {campaign: campaign});
this.$el.html(template);
}// end success
}); // end fetch
}// end if option.id
} // end render function
}); // end campagin view
return CampaginView;
});

In your View you are specifying an array of dependencies, which will be passed to the definition function as function arguments, listed in the same order as the order in the array. But you only declared 4 arguments: $ (jQuery), _ (underscore), Backbone and campaignTemplate(which is wrong because according your dependencies should be RewardView). So you have to declare properly your functions. For example:
define([
'jquery',
'underscore',
'backbone',
'views/RewardView',
'views/FriendRewardView',
'models/CampaginModel',
'text!templates/backbone/portal/campaignTemplate.html'
], function($, _, Backbone, RewardView, FriendRewardView, Campagin, campaignTemplate){
...
}
Check the documentation of Require JS for more info.

Related

Uncaught TypeError: this.model is not a constructor in backbone.js

I am new in Backbone.js. I just started a project with backbone.js. But i am faceing some issues. my model is :
define([
'underscore',
'backbone'
], function(_, Backbone) {
var Event = Backbone.Model.extend({
urlRoot: '/event',
idAttribute: 'eventID',
defaults: {
eventID: null
, eventKind: null
, eventTypeID: null
, eventDate: null
}
});
return Event;
});
Collection is :
define([
'jquery',
'underscore',
'backbone',
'../newModels/event'
], function($, _, Backbone, Event){
var event = new Event();
var Events = Backbone.Collection.extend({
url: '/event',
model: event,
orderBy: "eventID",
});
return Events;
});
and my View is :
define([
'jquery',
'underscore',
'backbone',
'../newModels/event',
'../newCollections/events',
'text!templates/Template.html'
], function($, _, Backbone,Event,EventsCollection,Template){
var sView = Backbone.View.extend({
el: $("#contentView"),
render: function(){
var eventsCollection = new EventsCollection();
var data = [];
eventsCollection.fetch({
success: function(collection,response){
alert("REST Service call was success");
alert(JSON.stringify(collection.models));
})
}
});
return sView;
});
But when execute the eventsCollection.fetch() in render function, i am getting the following error :
Uncaught TypeError: this.model is not a constructor
at F.d._prepareModel (backbone-min.js:24)
at F.d.add (backbone-min.js:19)
Please help me to find out the issue/my mistakes in coding. Every help will be appreciable.

Using backbone and passing a parameter to a model

I'm using backbone.marionette for view control.
My issue is "How do you pass a parameter to a model?"
This is what I have tried:
define([
'jquery',
'underscore',
'backbone',
'models/CampaginModel',
'collections/CampaignCollection',
'text!templates/includes/_campaign.html'
], function ($, _, Backbone, CampaginModel, CampaignCollection, campaignTemplate) {
var campaginView = Backbone.Marionette.ItemView.extend({
template: campaignTemplate,
initialize: function (options) {
this.campaign_id = options.id;
},
model: CampaginModel({id: this.campaign_id}),
onRender: function () {
}
}); // end campagin view
return campaginView;
});
I have noticed that my parameter get passed to the view init function I'm kinda stuck after this point. In standard backbone I just created a new model in the render function and passed the parameter to the model that way. However Marionette views have a 'model' attribute which I think should allow me to pass in it there, but it does not!
Model:
define([
'underscore',
'backbone',
'jquery'
], function (_, Backbone, jquery) {
var CampaginModel = Backbone.Model.extend({
urlRoot: '/api/v1/campaign/',
// Model Constructor
initialize: function () {
},
});
return CampaginModel;
});
I don't know what your file structure looks like.
But it should be like something like this.
define([
'jquery',
'underscore',
'backbone',
'models/CampaginModel',
'collections/CampaignCollection',
'text!templates/includes/_campaign.html'
], function ($, _, Backbone, CampaginModel, CampaignCollection, campaignTemplate) {
var campaginView = Backbone.Marionette.ItemView.extend({
template: campaignTemplate,
initialize: function (options) {
this.campaign_id = options.id;
this.model.set({id: this.campaign_id});
},
model: CampaginModel,
onRender: function () {
}
}); // end campagin view
return campaginView;
});
I haven't test the code yet.
If you need to set your parameters to model, you have to use backbone's model.set() function

Backbone.js Subview rendering

I am trying to get to grips with Backbone.js and have been following the 'modular' approach outlined by Thomas Davis here.
I seem to be stuck when trying to 'include' a view within another view as follows:
SettingsView.js
define([
'jquery',
'underscore',
'backbone',
'text!templates/settings/settingsTemplate.html'
], function($, _, Backbone, settingsTemplate){
var SettingsView = Backbone.View.extend({
el: $("#interface"),
render: function() {
this.$el.html(settingsTemplate);
}
});
return SettingsView;
});
ScriptView.js
define([
'jquery',
'underscore',
'backbone',
'views/settings/SettingsView',
'models/script/ScriptModel',
'collections/scripts/ScriptsCollection',
'text!templates/script/scriptTemplate.html'
],
function($, _, Backbone, SettingsView, ScriptModel, ScriptsCollection, scriptTemplate) {
var ScriptView = Backbone.View.extend({
el : $("#container"),
render : function() {
this.$el.html(scriptTemplate);
//add the settings view
var settingsView = new SettingsView();
settingsView.render();
}
});
return ScriptView;
});
UPDATED: I have managed to fix the error which I have just realised as to why that was happening (params were in the wrong order - DOH!!)
I no longer get the error but my 'SettingsView' is still not appearing. When I console log this inside 'ScriptView.js' I see that 'el' is undefined so my thinking is that this is where the issue may be...?
Thanks
I think you have to append the SettingsView to the el of the ScriptView:
var ScriptView = Backbone.View.extend({
el : $("#container"),
render : function() {
this.$el.html(scriptTemplate);
//add the settings view
var settingsView = new SettingsView();
settingsView.render();
this.$el.append(settingsView.el);
}
});
And a great post about subviews is this one.
I hope it helps!

collection not getting any response from instagram url

I'm reading in json data from an instagram url in a collection, and fetching the collection data in a view, but not getting any response. What's wrong?
Model:
define([
'underscore',
'backbone'
], function(_, Backbone) {var TopListModel = Backbone.Model.extend(return TopListModel;});
Collection:
define([
'jquery',
'underscore',
'backbone',
'models/topList/topListModel'
], function($, _, Backbone, TopListModel){
var TopListCollection = Backbone.Collection.extend({
model: TopListModel,
url: 'https://api.instagram.com/v1/tags/'+hashtag+'/media/recentaccess_token='+access_code,
});
return TopListCollection;
});
View:
define([
'jquery',
'underscore',
'backbone',
'text!templates/sidebar/topList.html',
'collections/topList/TopListCollection',
//'text!templates/home/homeTemplate.html'
], function($, _, Backbone, topList, TopListCollection){
var TopListView = Backbone.View.extend({
el: $("#sidebar"),
initialize: function(){
_.bindAll(this, 'render');
var topListCollection = new TopListCollection();
topListCollection.fetch({success: function(collection, data){
console.log(data);
}});
//this.render();
},
define([
'underscore',
'backbone'
], function(_, Backbone) {var TopListModel = Backbone.Model.extend(return TopListModel;});
Should probably be:
define([
'underscore',
'backbone'
], function(_, Backbone) {var TopListModel = Backbone.Model; return TopListModel;});
Better yet, unless you're doing something special in the model, you don't need to actually define it. Backbone will just create a model based off of the response.

Nesting Dependency Backbone.js Views with Require.js Backbone.js Leads to View Loaded as Object Instead of Function

I'm using Require.js with Backbone.js and Underscore.js, and I have a nested view that is coming up as undefined when called as a dependency, but when I have the two views in the same module, they work fine. I'm wondering what I'm doing wrong. Here's an example:
child-view.js
define([
'jQuery',
'Underscore',
'Backbone',
], function ($, _, Backbone) {
var ChildView = Backbone.View.extend({
initialize: function () {
_.bindAll(this, 'render');
this.render();
},
});
return ChildView;
});
parentview.js
define([
'jQuery',
'Underscore',
'Backbone',
'src/views/child-view'
], function ($, _, Backbone, ChildView){
var ParentView = Backbone.View.extend({
initialize: function () {
_.bindAll(this, 'render');
this.render();
},
render: function () {
child = new ChildView({});
}
});
return ParentView;
});
I receive a "Uncaught TypeError: undefined is not a function" when trying to call the new ChildView. If I reference the ChildView outside of the Parentview but inside of parentview.js, it displays the view, but as an object.
Just from your code, there should be no problem, I tested your code actually did not find the problem.
This is my test code,you can try it:
http://files.cnblogs.com/justinw/test_byfejustin.zip
I think it might be your “require.js” have a problem,you can replace your "require.js" with my "test_byfejustin\js\libs\require\require.js" in my code package,and try again.
Variable names are case-sensitive. In child-view.js you are returning "ChildView" which is undefined (you've assigned childView).

Categories