Restangular: getList(), then get related information - javascript

I am trying to wrap my head around how this would be done.
Get a list of "Creations".
"Creations" list returns an ID of the "Author" of that creation.
Automatically do another Restangular call to grab that Authors information and relate it to the object returned by the first getList of creations.
Restangular.all('creations').getList({
limit: 5
}).then(function(creations) {
$scope.creations = creations;
// returns a list of Objects for each creation with one parameter being author:id (author:1,2,3, etc)
// THEN GET INFORMATION ABOUT EACH CREATION'S AUTHOR
// Restangular.one('users', creations.author).get().......?
});
Is there a better way of doing this so that in my view I can access something like $scope.creations.authorObject...

Related

I want to store changeable data within one column of my table row

So, I have a vue component called stats. Basically how its stored is it has a name for example: 'goals'. It fetches the row with the id/name of 'goals' from the database and the point is to access the info column within that to fetch specific data.
This is how the row would look
ID Name Info Created_at Updated_at
"1" "user" "{"goal": 1,"current":500}" "2019-04-28 03:54:44" "2019-04-28 03:54:46"
I want to be able to access the data within the Info column and display it accordingly. I can't seem how to get it working though within javascript.
This is how i call it
axios.get('/api/stats/user').then((res) => {this.test = res.data; console.log(this.test)});
and in the console i get the object i want but info looks like this:
info: "{"goal": 1000, "current":500}"
and I can't seem to be able to grab it by ['goal'] or this.test.info.goal which is what I want
First of all, create a resource file for yor model and create an accessor for your info/goal field.
You should use the resource files for each json response which contains models/collection of models.
This way you can modify what to display in your json response about each model.

nodejs Cycle and callback

Recently i wrote a blog with nodejs,when it comes to add some tags to posts,problems occured.In my view, the relation between posts and tags is many-to-many,each post has several tags,in turn,each tag also match to some posts.So i create a tag collection and a post collection in mongodb.
exports.Post=mongolass.model("Post",{
author:{type:Mongolass.Types.ObjectId,ref:"User"},
title:{type:"string"},
content:{type:"string"},
pv:{type:"number"},
tagIds:[{tagId::Mongolass.Types.ObjectId}]
});
exports.Tag=mongolass.model("Tag",{
tagName:{type:"string"}
});
exports.Tag.index({tagName:1},{unique:true}).exec();
and store the tag as an array in posts.As we know,when i create a post, i will get the post's tagName from html by method POST,so i have to use
getTagByName:function getTagByName(tagName){
return Tag.find({tagName:tagName}).exec();
}
to get the object tag,and use
TagModel.getTagByName(tagName).then(function(tag){
var postToTag={
postId:postId,
tagId:tag[0]._id
};
to get the tagId,Unfortunately, as a post has many tags,so i have to use a for cycle to get all tagId of a post, i also want to collect these tagIds to an Array, thus i can push it to my Post model, but the cycle and callback make it difficult.
any advice is appreciated.

Can't display javascript object properties in mandrill template using handlebars

I am trying to display dynamic content by using the mandrill template api in a node project.
I have followed the docs and looked at plenty of examples, and for the most part can get things working.
However, when I try to access properties of an object that I pass through the api in the mandrill template, it does not display anything.
Here is my mandrill template (using handlebars):
<p>{{greeting}} {{person.firstName}},</p>
<p>{{greeting}} {{person.0.firstName}},</p>
<p>Your location is {{person.location}}.</p>
Now, the greeting does display the value passed in the global_merge_vars part. But the properties for the person object do not get displayed, as if they are undefined.
Here is part of the json being sent:
var greeting = "Hello ";
var person = {firstName:"testfname",location:"testlocation"};
var globalMergeVars = [
{"name": "greeting","content":greeting},
{"name": "person","content": person},
];
Am I not passing the object correctly or naming the 'name' property correctly in the api call? I have tried a bunch of different things. I know that I could create multiple vars inside the globalMergeVars object instead of passing the entire person object, however I have a lot more properties attached to the person object.
I have also successfully used an each loop for an array of items and that all gets displayed correctly.
Thanks.

Fetch Backbone Collection by Model IDs list

I have a REST API serving a few URLs:
/rest/messages
provides all messages. A message is a JSON/Backbone Model
{
title: 'foo',
body : 'bar'
}
To get a single message I have:
/rest/messages/:id
Is it possible to fetch a Backbone Collection using message IDs array? I don't want the whole message list, but just a few messages I specify by ID.
I could fetch Models one-by-one and fill up the Collection, but I'm wondering if Backbone has a cleaner way to do this.
Thanks
According to documentation, you can pass ajax options to the fetch call. So, you can pass ids as data attribute to the fetch call being done and based on it, return the respective models from the server.
For example (when doing fetch),
collection.fetch({
data : {
message_ids : [1, 3, 5] // array of the message ids you want to retrieve as models
}
})
This message_id array will be accessible as parameters (not sure of the name in your case) in the server code being executed at /rest/messages, from there you can return only specific models based on ids you receive as message_ids. The only thing you need is, client side must be aware of the ids of all the message models it needs.
You can use any data structure instead of array to send message_ids.
The url property of collection reference to the collection location on the server. When you use fetch, backbone uses that url.
The url property can be also a function that returns the url. So you can do something like that:
var ids = [1,2,3]
var messages = new MessegecCollection();
messages.url = function() {
return "/rest/messages/"+ids.join("-"); //results "/rest/messages/1-2-3"
}
messages.fetch();
You can also create a method in your collection that generated and set the url, or even fetchs a set of models.
Now all you have to do is to support this url: /rest/messages/1-2-3
Hope this helps!

How do I get a model from a Backbone.js collection by its id?

In my app, everything I do with data is based on the primary key as the data is stored in the database. I would like to grab a model from a collection based on this key.
Using Collection.at() requires the array index, Collection.getByCid() requires the client ID that backbone randomly generates.
What is the best way to grab the model I want from the collection with the given id value? I figure the worst I could do would be to iterate over each item, .get('id'), and return that one.
Take a look at the get method, it may be of some help :)
http://backbonejs.org/#Collection-get
get collection.get(id)
Get a model from a collection, specified by an id, a cid, or by passing in a model.
If your data requires you to use a different kind of key or a set that doesn't mesh well with at(), getByCid() or get(), there is also where(). Something like this might work:
window.lib = new Library;
window.lib.fetch([
success: function(model, response) {
console.log(window.lib.where({'BookID':488, 'Rev':2, 'Status':'Active'});
}
});

Categories

Resources