I'm trying to simply return what I request in PHP to JSON.
My problem is that each Stock is not yet completed.
Indeed, it is the "render" but "this.collection.models" is not yet completed because the request is not yet finished.
What should I do to correct this problem, wait until the request is finished so that the loop is done correctly.
Thank you in advance
var Article = Backbone.Model.extend({});
var Articles = Backbone.Collection.extend({
model:Article,
url: function() {
return _BASE_URL+'/sync/getLastArticles';
},
initialize:function () {
this.fetch();
}
});
var ArticlesView = Backbone.View.extend({
template:$('#articles').html(),
initialize:function () {
this.collection = new Articles();
this.render();
},
render:function () {
console.log(this.collection);
var that = this;
_.each(this.collection.models, function (item) {
console.log(item);
}, this);
},
renderArticle:function () {
;
}
});
You render before the fetch is done. What you want to do, is to wait for the fetch to complete and then render. Now how would you get notification of when the fetch is done? You have 2 options:
The success function (Not recommended by me)
// ArticlesView
initialize: function() {
_.bindAll(this); // Don't forget to BIND
this.collection = new Articles();
this.collection.fetch({
success: this.render
});
}
Now when the fetch has been successful, render is called. This however can cause scoping problems and Backbone.js offers a much nicer alternative to callback functions: events.
Event callback (prefer this)
// ArticlesView
initialize: function() {
_.bindAll(this);
this.collection = new Articles();
this.collection.on('reset', this.render); // bind the reset event to render
this.collection.fetch();
}
Related
I have some functionality in a onRendered event that I want to run on every render.
Template.DashboardCoachPost.onRendered(function () {
var post, postId;
this.autorun(function() {
postId = Router.current().params.query.p;
reactive.isSubmitted.set(false);
if (Template.instance().subscriptionsReady()) {
post = Posts.find({_id: postId}).fetch()[0];
reactive.post.set(post);
if (post) {
reactive.isSubmitted.set(true);
}
}
});
});
In my RouterController I have:
DashboardCoachPostController = RouteController.extend({
subscriptions: function() {
this.postId = Router.current().params.query.p;
if (this.handle) {
this.handle.stop();
}
if (this.postId) {
this.handle = this.subscribe('posts', { postId: this.postId });
}
}
});
and my route:
Router.route('/dashboard/coach/post', {
name: 'dashboardCoachPost',
controller: DashboardCoachPostController,
where: 'client',
layoutTemplate: 'LegalLayout'
});
I have a feeling that I am not handling subscriptions properly, but I can't figure out how to get my post without this method.
Template.instance().subscriptionsReady() is used when you subscribe in a template instance. But you are subscribing in the router.
You have to choose if you let the template instance handle the subscription or the router.
This below is my collection code
Competitions = new Mongo.Collection("competitions");
var CompetitionsSchema = new SimpleSchema({
year: {
type: String
},
division: {
type : String,
allowedValues: ['Elite', '1st','2nd','3rd','4th','Intro']
},
teams:{
type : [TeamSchema],
allowedValues: (function () {
return Teams.find().fetch().map(function (doc) {
return doc.name;
});
}()) //here we wrap the function as expression and invoke it
}
});
In the allowedValues function
Teams.find is empty.
In the router I am subscribing to the publication as follows
this.route('competitions', {
path: '/admin/competitions',
layoutTemplate: 'adminLayout',
waitOn: function () {
return [
Meteor.subscribe('teams')
];
}
});
This is my publish function
Meteor.publish('teams', function() {
return Teams.find({},{sort: {
points: -1,
netRunRate : -1
}});
});
Do I have to do subscription some where else as well?
Your problem is in this piece of code:
allowedValues: (function () {
return Teams.find().fetch().map(function (doc) {
return doc.name;
});
}()) //here we wrap the function as expression and invoke it
This gets called when the page loads. At that point the Teams collection will still be empty on the client side. You need to wait until the data is ready. Since you are using waitOn in iron-router, it might be enough to just move this code to the onRendered callback.
I'm building a meteor app and on one route I'm adding multiple data context like so -
this.route('orgPage', {
path: '/org/:title',
data: {
orgs: function () {Orgs.findOne(this.params._id)},
projects: function() {Meteor.subscribe('projects', this.params._id)}
}
The only problem is that when I try to access this data in my templates js file, I can't access the _id or any of the attributes of orgs.
I've tried several approaches, but it always returns undefined. If I use a single data context, it works perfectly. Here is the function that doesn't function properly -
Template.orgPage.events({
'click #newProject': function(e) {
$('#npModal').modal();
},
'submit #npModal form': function(e, template) {
e.preventDefault();
if(!$(e.target).find('[name=newTitle]').val()) {
var projectTitle = 'Untitled'
} else {
var projectTitle = $(e.target).find('[name=newTitle]').val()
}
var theid = this._id;
var newProject = {
title: projectTitle,
organization: theid
}
Meteor.call('project', newProject, function(error, id) {
if (error)
return alert(error.reason);
$('#npModal').modal('hide');
$('#npModal').on('hidden.bs.modal', function (e) {
Router.go('newFields', {});
})
});
});
Anyone have any ideas? Thanks!!
You have missed a return statement. function () {Orgs.findOne(this.params._id)} should be function () {return Orgs.findOne(this.params._id)}. Further more, this inside this function won't refer to what you want, so you can't use this.params. And why do you subscribe to a subscription as a data context property? Do it in the waitOn function instead.
I have asked this question on the google forum for AngularJS and haven't heard about it until now. Can someone help me understand what is going on here?
I am trying to periodically refresh a resource and it doesn't seem to be working. I have tracked it until the fact that the promise has been obtained from $http service but the XHR request is never created and fired when the method is invoked in setTimeout. However, if I do the same without setTimeout everything seems to be working just fine.
Working JSFiddle: http://jsfiddle.net/hponnu/Z62QN/2/
window.root_module = angular.module("MyApp", ['ngResource']);
function MainController($scope, $resource) {
$scope.buttonClick = function () {
var res = $resource("http://www.google.com");
res.get({}, function (response) {
alert("response");
}, function (err) {
alert("error");
});
}
}
Broken JSFiddle: http://jsfiddle.net/hponnu/H8aEt/10/
window.root_module = angular.module("MyApp", ['ngResource']);
window.count = 0;
function MainController($scope, $resource) {
$scope.buttonClick = function () {
setTimeout(function () {
alert("timeout: " + window.count);
var res = $resource("http://www.google.com");
res.get({},
function (response) {
alert("response: " + window.count);
window.count++;
}, function (err) {
alert("error: " + window.count);
window.count++;
});
}, 1000);
}
}
As you will clearly see in the broken jsfiddle the error alert is not fired for the first request unless a click event is triggered by click on the button again. I have started noticing this from AngularJS 1.1.4
Any ideas/suggestions?
PS: https://groups.google.com/forum/#!topic/angular/t28mazamT0E is the link for the Google groups thread.
You should always use Angularjs's $timeout instead of setTimeout().
function MainController($scope, $resource, $timeout) {
$scope.buttonClick = function () {
$timeout(function () {
...
}, 1000);
}
}
The following code does not update the database everytime a tweet is found - it silently hangs, adding no tweets to the database.
If a tweet is manually added to the DB from the JS console in the browser, it shows up just fine, but no tweets are being added to the DB automatically.
Tweets = new Meteor.Collection("tweets");
if (Meteor.isClient) {
Template.kildeer.tweets = function () {
return Tweets.find({});
};
}
if (Meteor.isServer) {
Meteor.startup(function () {
var require = __meteor_bootstrap__.require,
Twit = require('twit')
, T = new Twit({
consumer_key: 'blahblah',
consumer_secret: 'blahblah',
access_token: 'blahblah',
access_token_secret: 'blahblah'
});
var stream = T.stream('statuses/filter', { track: ['bing', 'google', 'microsoft'] })
stream.on('tweet', function (tweerp) {
var id;
console.log(tweerp.text);
id = Tweets.insert({text: tweerp.text, screen_name: tweerp.user.screen_name, profile_image: tweerp.user.profile_image_url});
console.log(id);
});
});
}
In Meteor, Collection.insert must always be called inside of a Fiber() closure.
Fiber(function() {
Tweets.insert({text: tweerp.text, screen_name: tweerp.user.screen_name, profile_image: tweerp.user.profile_image_url});
}).run();