I am working with the new Alloy Framework on Appcelerators Titanium and I am totaly lost with its backbone handeling.
Anyway, my Problem is that I got a deep multi level json object which looks something like this:
[{
title: "Bla",
id: 0,
content: "lorem ipsum dolor sit amet..",
articles: [
{
title: "bla2",
content: "bla bla bla",
nr: "article 1"
},
{
title: "bla3",
content: "bla baasdadla bla",
nr: "article 2"
}
]
},
{
title: "Bla 2",
id: 1,
content: "lorem ipsum dolor sit amet..",
articles: [
{
title: "bla3",
content: "bla bla bla",
nr: "article 10"
},
{
title: "bla4",
content: "bla baasdadla bla",
nr: "article 11"
}
]
}];
And I successfully implemented it so that I got a nice TableView with the titles of the first level, but now I need access to the 2nd level, and this is where I fail at.
I got everything in a single Collection
var myCollection = Alloy.createCollection('bla');
myCollection.add(MyBigBigBigJson); // see above
myCollection.fetch();
my View.xml:
<Alloy>
<Collection src="bla">
<TableView dataCollection="bla">
<TableViewRow>
<Label text="{title}" />
</TableViewRow>
</TableView>
</Alloy>
Now, how do I get access to, lets say the content of the 2nd level?
I tried it with:
myCollection.at(0) // first object in my collection ( where title = "Bla" )
myCollection.at(0).articles[0].content // gives me an error and my app crashes...
myCollection.at(0).articles // undefined
Well, I have no idea how I get to the articles and then to the content or title.
Anyone can help me? I hope I've made everything clear.
Thanks!
You need to use model.get:
myCollection.at(0).get("articles")[0].content
Another approach that you can take is to use toJSON() on your model to get to the properties.
myCollection.at(0).toJSON().content
at(0) specifies the index of the model and you're using to JSON() to just send back the attributes. Then you can just specify the property from there.
Related
Working with a frontend variable like below in javascript with many objects... (Note - the scores would be different for each user therefore I would need to be able to get the info from the frontend)
var campgrounds = [{ name: "State Park #1" score: 0, }, { name: "State Park #2" score: 0, }, { name: "State Park #3" score: 0 }]
How would I be able to store that data in MongoDB? Could I somehow loop through the data? Or could I store the entire variable "campgrounds" in Mongo? like $("#quizData").value(campgrounds)
Would I set up a Schema like below? Even if I did I'm not sure how to actually get the data in the variable into Mongo. Not sure how to accomplish my goal. Thanks for any help! :)
var campgroundsSchema = new Schema ({
"campgrounds" : [{
name : {type: String},
score : {type : Number}
}],
});
If you connect to mongo using mongoshell ...
mongo --host localhost:27017
you can issue the following javascript commands...
use campgroundsdb
var campgrounds = [{ name: "State Park #1", score: 0 }, { name: "State Park #2", score: 0, }, { name: "State Park #3", score: 0 }]
db.campgroundscollection.insertMany(campgrounds)
Then find them by issuing...
db.campgroundscollection.find().pretty()
If you want to create a program written in JavaScript it will need the ability to connect to the mongodb process. Node.js tutorials fit this requirement...
https://www.w3schools.com/nodejs/nodejs_mongodb.asp
I've been unable to add an itemclick listener to what I thought was a very simple example. The view is shown correctly with the repeated HTML. Can anyone help with where I've gone wrong?
Ext.define('App.view.Stuff', {
extend: 'Ext.DataView',
alias: 'view.stuff',
store: [{
"title": "Dataset 1",
"desc": "Lorem ipsum dolor sit amet."
}, {
"title": "Dataset 2",
"desc": "Lorem ipsum dolor sit amet."
}],
listeners: {
itemclick: function () {
console.log('itemclick');
},
},
itemTpl:
'<div><strong>{title}</strong></div>' +
'<div>{desc}</div>'
});
Since v6.5.0 the event is called childsingletap.
childsingletap ( this, location, eOpts )
Fires when a child is single tapped.
Available since: 6.5.0
Parameters
this : Ext.dataview.DataView
This dataview.
location : Ext.dataview.Location
The location for the event.
eOpts : Object
The options object passed to Ext.util.Observable.addListener.
Here's the working code:
Ext.define('App.view.Stuff', {
extend: 'Ext.DataView',
alias: 'view.stuff',
store: [{
"title": "Dataset 1",
"desc": "Lorem ipsum dolor sit amet."
}, {
"title": "Dataset 2",
"desc": "Lorem ipsum dolor sit amet."
}],
listeners: {
childsingletap: function () {
console.log('childsingletap');
},
},
itemTpl:
'<div><strong>{title}</strong></div>' +
'<div>{desc}</div>'
});
Before I start coding I'm trying to figure out how I'd do it first. So what I want to do is to display a list of articles, by simply loading it from a database for example: getAllArticles().
The list will display all the titles from the articles. Nothing difficult about that.
However, there's a chance to comment on that article so all the comments will be stored in a new database tabel with the ID of the article.
I'll probably have to load them with getAllCommentsById() but how do I display the amount of comments next to each article in an ng-repeat?
I don't have any code yet, but let's say it will look like this:
Angular
<div ng-repeat='article in articles'>
<p>{{amount}}</p>
<p>{{article.title}}</p>
</div>
$scope.articles = getAllQuestions();
Article database
{
"id":1,
"title": "Article 1"
}
{
"id":2,
"title": "Article 2"
}
Comments database
{
"id":1,
"comment": "Lorem ipsum",
"article_id": 1
},
{
"id":2,
"comment": "Lorem ipsum",
"article_id": 1
},
{
"id":3,
"comment": "Lorem ipsum",
"article_id": 2
}
I think that you can do it in two several ways.
You can add a counter of the comment relatives to an article in the articles object
Create a function in angular's controller that give the number of comments of an article
$scope.countComments = function(articleId) {
...
}
Create an angular service that count the comments of each article
Hi There Beste Collegues,
I've having an problem building my json output structure well.
What i want is the following output:
{
comments: {
data: {
created_at: "date",
other: "etc",
from: {
username: "navidos",
user_id: 1
}
}
}
}
But the way it is now build is:
{
data: {
description: "post description",
status: "public",
link: "http://www.bla.com",
created_at: "2015-01-23 00:00:00",
comments: [
{
text: "comment text 1",
date: "2015-01-23 18:30:00",
id: 1
},
{
text: "comment text 2",
date: "2015-01-23 18:35:00",
id: 2
},
{
text: "comment text 3",
date: "2015-01-23 19:00:00",
id: 3
}
],
user: {
user_id: 1,
username: "navid",
profile_picture: null,
bio: null
}
}
}
The output i get is almost fine but i want the comment section in this output like the first output. I've tried everything with array_merge and array push but can't figure out what i'm doing wrong here. Is there anyone who can help me out. I'm using Laravel 4 and doing my Relations with the Eloquent ORM.
So in my route i've now the following.
$post = Post::find($id);
$postComments = $post->comments;
$usersPost = $post->user;
return Response::json(
$data = array('data'=>$post)
);
I would be very grateful if someone can help me out with this. Tnx in advanced.
You could simply name your relationship to User in Comment from():
public function from(){
return $this->belongsTo('User');
}
Then eager load comments and from:
$post = Post::with('comments.from')->find($id);
This should result in something like this:
{
description: "post description",
status: "public",
link: "http://www.bla.com",
created_at: "2015-01-23 00:00:00",
comments: [
{
text: "comment text 1",
date: "2015-01-23 18:30:00",
id: 1,
from: {
user_id: 1,
username: "navid",
profile_picture: null,
bio: null
}
}
// next comment
]
}
If you want to hide some of these attributes in your JSON output you can add them to the $hidden array in the model:
class Comment extends Eloquent {
protected $hidden = ['id'];
}
What i currently have in my Post.php model is:
//An post belongs to an user
public function user(){
return $this->belongsTo('User')->select(array('user_id', 'username','profile_picture','bio'));
}
//An post has many comments
public function comments(){
return $this->hasMany('Comment')->select(array('comment as text','date','comment_id as id'));
}
and my Comment.php model looks like:
//An post belongs to an user
public function user(){
return $this->belongsTo('User')->select(array('user_id', 'username','profile_picture','bio'));
}
and my User.php model looks like:
public function from(){
return $this->hasMany('comments');
}
in my Route now i having the following like you suggest:
$post = Post::with('comments.from')->find($id);
but this throws an error:
Missing argument 1 for Illuminate\Database\Query\Builder::from()
is there something i'm not doing correct?
My template:
<div ng-repeat="comment in article.comments">
{{ comment.date }} by {{ comment.author }} <button type="button" ng-click="removeComment($index)">remove</button>
</div>
My JSON comments:
"comments": [
{
"author": "Syl A",
"date": "2014-07-02",
"content": "lol"
},
{
"author": "Syl B",
"date": "2014-07-02",
"content": "lol"
},
{
"author": "Syl C",
"date": "2014-07-02",
"content": "lol"
}
]
My controller:
$scope.removeComment = function (key) {
// This is what I want to remove
// Object {author: "Syl A", date: "2014-07-02", content: "lol", $$hashKey: "004"}
console.log($scope.article.comments[key]);
};
The following snippets is not OK, it does nothing;
$scope.article.comments.splice[key, 1];
The following snippets are not OK, the object is removed but in the view got "by remove", the complete line is not removed and I can't remove more than one item Error: [ngRepeat:dupes]:
$scope.article.comments[key] = undefined;
delete $scope.article.comments[key];
The following snippet is not OK, the object is removed but in the view got "by remove", the complete line is not removed:
$scope.article.comments[key] = {};
So I didn't find a solution on SO to make an "all-in-one" remove, DOM and "data". Why my splice doesn't work here?
You're using splice wrong is the problem.
Update your splice to use ( ) instead of [ ]
$scope.article.comments.splice(key, 1);