find() vs findOne() with for loop for retrieving all - javascript

Is there any speed/use/readability difference between find() vs findOne() with for loop if i want to retrieve all documents?
I want to make my code more reusable, and i have code which is populating my blog Post with and Author, but now I have to populate it in two different places, in homePage controller (shows all posts, uses find()) and in single post controller (findOne())

Related

how can I loop through returned Resource object data using angular.forEach loop in controller

I came across a similar scenario, but my attempts failed because undefined was always returned and kept on getting error after error in the console.
The data is retrieved from the openweathermap API using the angular $resource factory. I understand that i can target and get some of the data i need in the view using ng-repeat but what I also want to break down this data in the controller and access some of the keys and values. When I console.log($scope.weatherResult) variable that holds the data retrieved from the api I get a Resource object in the console.
How can I go about looping through this object to retrieve the data I need. Its basically objects within objects within arrays. In the view ng-repeat does wonders to get this data but in the controller I don't know how to retrieve it. For example if I console.log($scope.weatherResult.city) or console.log($scope.weatherResult.list) the result in the console is always undefined.
Now the data I want to retrieve is within the list array - two days worth of data, but the first object list[0].temp has temperature figures, I want to calculate the sum of those figures and divide it and get the average so I can store it in another variable in the controller and output it in the view.
I'm not sure if im taking a long route here, but i've been in similar situations before but managed to get away with it using the ng-repeat in the view. I need to learn how to do this.
Is it possible to loop through the resource object using angular.forEach() function and get the data in the temp object? I have attached an image for reference. I am still learning and gaining experience with JavaScript and angular in particular.
$scope.temp = []
angular.forEach(list[0].temp, function(value,key){
$scope.temp.push(value);
})
See if this helps you.

Strategy for testing POST to API without changing database

I'm using jasmine-node to test my API, and it has worked great for my GET routes. Now, however, I need to test some POSTs and I'm not sure how to go about this without changing my database.
One thought I had was to reset whatever value I change at the end of each spec.
Is this reasonable or is there a better way to go about testing POST requests to my API?
Wrap anything that modifies your database into a transaction. You can have your database changes and then rollback after each test.
usually you are supposed to have a test database, so modify that one is not a big issue. also, a general approach would be not to rely on predefined values on the database (i.e, the GET always request the SAME object..) but try with different objects each time. (using predefined objects may hide problems when the data is slighty different..).
in order to implement the second strategy, you can execute a test with a POST with pseudo-random data to create a new object, and use the returned ID to feed the following GET, UPDATE and finally the DELETE tests.
Just make a duplicate processing page/function and send the data to that for debugging. Comment out anything that makes changes to the database.
Alternatively, pass a variable in your call such as "debug" and have an if/else section in your original function for debugging, ignoring the rest of the function.
Another alternative still is to duplicate your database table and name it debug table. It will have the same structure as your original. Send the test data to it instead and it won't change your original database tables.
I'm pretty sure that you've come up with some solution for your problem already.
BUT, if you don't, the Angular $httpBackend will solve your problem. It is a
Fake HTTP backend implementation suitable for unit testing applications that use the $http service.

Ember.js: Adding item to an ArrayController without creating it first?

I have been told time and time again to "never use .create() on a controller/ObjectController because Ember will do it for [me]". I am confused as to how I then can add an item to an ArrayController without first creating it.
In my case, I am using a Controller (not an ObjectController) as an itemController for an ArrayController since it is not backed by AJAX-loaded data (...is this the correct usage?). I would like to add an instance of my itemController to the ArrayController, but I can't figure out how to do that without first creating the item to be added.
JSBin here:
http://emberjs.jsbin.com/momikuto/17/edit
TL;DR: I don't know how to add a Post object to the ArrayController without the .create() in the addPost function.
Models vs Controllers
I think the primary confusion here is between models and controllers. A controller can decorate a model to add display attributes but isn't the primary data object.
A model can be a simple JSON object, or it could be full-blown ember-data.
So, yes Ember will create your controllers for you. And you specify the model(s) for the controller somehow (see below).
Setting Controller Data
So in your example you are trying to seed your controller with two static posts id:1, id:2. This is probably best done in the Route as opposed to the View.

How do you limit results from a findAll in ember.js?

My api takes query parameters such as "page". This is to limit the number items sent over. I realize you can filter these with ember once they've already been retrieved, but I want this app to scale. This means I can't be receiving large amounts of data over the wire like findAll already does. How would I go about doing this in ember? Where should I put my custom ajax? I still would like to use most of the features ember has like the ability to watch my models.
This sounds like you want pagination support, but lamentably this feature is still not implemented in ember core. However some folks have already created pagination support to work with ember, one very good example is from Toran Billups, have a look here: https://github.com/toranb/ember-pagination-example. Have also a look here for a good answer explaining it's implementation: Ember pagination full example
Hope it helps.
Perhaps I misunderstand your question, but you can use App.Model.findQuery and pass it query string parameters, which will be sent to your server.
For example: App.Model.findQuery({page: 5, color: 'blue'}). Of course it's up to your API to implement the response to the query params.
See also:
what's the difference between find, findAll and findQuery in ember-data
findQuery source

Problems with Nested Objects

I'm trying to create a nested model with Backbone and have a couple of issues:
I'll follow with the example in the backbone documentation: say I have a Mailbox Collection, and each Mailbox can have a collection of Messages. I want to see and work on these nested lists in my app. First - I alter my Mailbox model as noted in the backbone documentation.:
var Mailbox = Backbone.Model.extend({
initialize: function() {
this.messages = new Messages;
this.messages.url = '/mailbox/' + this.id + '/messages';
this.messages.bind("reset", this.updateCounts);
},
});
My first question, when I populate data in the model with a server call (which I do with eager loading - I pass in the Mailboxes and nested Messages) I can load the data into attributes on each Mailbox, but when looking at it in a javascript debugger, I see the array of nested data in my object's attributes, but I don't see the data loaded into the actual nested collection. Am I doing something wrong? It's like there's two different sets of information.
Second question - when I show a nested index view - I want to show a list of Mailboxes each with a nested list of Messages - I try to use nested views with backbone, only my nested views repeat the views of other objects from the outer view - creating this strange grouping of duplicate views everywhere. I can't figure out what I'm doing wrong here. Might anyone have any pointers here?
Is there any good definitive guidance on creating not just models, but models, collections and views with nested objects in backbone?
I found out answers to both my questions on the IRC channel for documentcloud..
For the first question, it's simply an exercise in exposing that data. You can either create a global variable (of a collection or router) and expose the data there... That's just up to the user.
For the second question, I had an issue where I was using the rails-backbone gem, and when using the scaffolding for that gem to create nested views, you wind up using jquery to append views in multiple places, because the gem is not expecting to have multiple tbody tags within a document.

Categories

Resources