I'm using parse.com as my backend. I have 2 tables: post and comment. A post have multiple comments, so it is a one to many relationship. so I have comment point to the post (which is easy) but also an array on post that list all the comments this post have. I imagine if I used MongoDB or any other NoSQL it will be the same.
On front end, I have 2 modules: "post" and "comment". Each have a resource that is hooked with the backend. Each module also have all the CRUD pages that create, read, update and delete. When I write the view part, a post need to display all the comments below, so my post module depends on comment module and will use comment resource to find all comments for this post.
But when I write the create part for comment, I found myself need to update the post array and add the newly created comment to the array. Which means I need the post resource to update the post. The post resource is in the post module so now my comment module depends on post module and created a circular dependency.
I have 2 solutions I'm going to share, but I want to know if anyone have anything better:
I use angular-ui-router in my project. After a comment is created, I route back to a post state called "app.post.commentCreated" . This state is handled inside the post module. It will update the post array of comments.
I separate the post resource and comment resource to their own module called "postResource" and "commentResource" and make post and comment module depends on both. I have not done this yet. I'm about to.
Do you have anything better?
Thank you very much!
Related
It is a simple matter, but it got me curious.
I have a tags table with video_id and tag columns (people can create any tags).
Let's say a user wants to update the tags of one of their videos.
He will call an api and send the list of his new tags, and on the server side these tags will be checked. All tags that disappeared will be deleted from the table and all the new tags will be stored in their place.
Now the most obvious api call I can think of is PUT, however from the database viewpoint I am calling a POST/DELETE api, so it doesn't feel quite right.
Any api is probably gonna work just fine, but which is the one I should be using?
Answering to close the question!
As mentioned by #EspressoBeans it is ideal to use a PUT request for case scenarios like this.
Thanks!
After making a post request to add a blog, i then want to display those blogs on the same screen.
So there is no refresh.
What is the best way to get that updated list of blogs. Do i:
make another GET request to get the list of blogs and show a loading bar
just show the posted blog and assume the async POST will be successful
or is there a better way to tackle this?
Thanks
The best tactic really depends on the user experience and your opinion of what the new blog being on the screen might suggest to the user.
For some products, having the page move on immediately, and assuming the back-end is keeping up (eg for dragging a card to another column on a scrum board), will be the most pleasant for the user - no lag, and if the update it didn't go through and the card jumps back, no big issue.
For posting a blog though, a user might expect that if they can see their blog on the website, that that is confirmation of it going through the backend successfully. For that reason I think that another GET request is the better option, as it implicitly confirms to the user a successful POST.
It depends on the rest of your app and what you value.
The first one makes sense if you are using that same back-end controller in another part of your app that shows the blogs (maybe even before the blog is added) and you want to reuse it there.
The second one makes sense if you want to be network efficient and don't mind writing different back-end controllers for different back-end interactions. Also, you don't need to asume it will be successful, you need to catch errors and handle them.
An approach could be to do an empty post that returns the blogs but if the post has a new blog, then it is added and also returns all other blogs.
I am using dnn-8. I have one page which has Answers module. On another page I have added Discussion module. Now I require to add all the post from the answers module in the rightPane of the second page. I did that using existing module and it is showing as i require. But when I click on any topic url of discussion module to view its detail it gives 404 error. It is working perfectly if i remove the Answers module from right pane.
How to resolve that?
If you need any more info, please ask.
error snapshot
I believe that you are encountering a situation where two modules use a similar URL structure and the information is crossing. I believe that these modules need to remain on their own separate pages. Gien this, it isn't exactly possible to change.
I'm using a Spring Data REST backend with AngularJs as frontend.
How is the best way to add sub-resources to a "root Entity"?
From official documentation section 4.4.1:
http://docs.spring.io/spring-data/rest/docs/2.3.0.RELEASE/reference/html/#repository-resources.search-resource
we can POST to association resource using "text/uri-list"
A tipical example of sub-resource are Comments to a BlogPost like this question:
POSTing a #OneToMany sub-resource association in Spring Data REST
From Spring documentation I should proceed in two steps:
add the new sub-resource (POST a new Comment)
and then add the link (POST a text/uri-list to the BlogPost comments)
I don't see the relation with AngularJS : you can either use $http or $resource or halClient to POST your entities.
If you have a bi-directionnal relation between blogpost and comment, it should work just by POSTing the comments with the 'parentBlogPost' field filled. If you get back the blogpost, its comments would be updated automatically.
If you have uni-directionnal relation, then you need to POST the uri-list too.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I am building a RESTful web service using Laravel and a single page application on the front end with Angluarjs. Now where should I place the Angularjs files? Should I place them in the public folder of the Laravel installation or should I keep them completely separate since the I would be issuing calls to the resources in the web service and it would return JSON data and hence there is no need for them to be in the same place. What is the standard or the best practice?
Now, for the second part. How should I manage my routes and resources in Laravel if I am building a simple Todo application. This is where I get really confused and it is a little hard to explain but stay with me for a minute. For example I have a users resource at /users and I can fetch all users by issuing a GET request at /users or create a new users by issuing a POST request at /users. Similarly I can issue a GET request at /users/1 and fetch the first user and so on with other request verbs. Now, I have another resource called tasks. How should I implement this resource? Should I implement it like nested resource say /users/{user_id}/tasks. When I issue a GET request at /users/1/tasks, it will fetch all the tasks for the first user. But now it gets complicated because if issue a GET request at /users/10/tasks/1, should it fetch the first task for the 10th user. But then the implementation of such a logic becomes very difficult as I have to look for the first task of the 10th user.
I figured a way around this by only setting a GET route at /users/{user_id}/tasks which will obviously return all the tasks for the specified user. And then I would create a completely different resource /tasks to handle all the other request verbs. Am I doing this right?
And also what if I am using a NoSQL DB like MongoDB, in which case the data will be stored in such a manner that /users/{user_id}/tasks/{task_id} will not be difficult to implement since the tasks of every user will be in their own JSON object.
Am I thinking in the right direction? I am newbie and I don't exactly know what is the standard way to approach such database architecture problems? What are the best practices?
Here is my suggestion.
Basically you need to divide your application into modules. Say for e.g. login, feature 1, feature 2 etc. Now you should have folder for each module where you can keep all the files related to it (controllers.js, services.js, index.html, style.css, filters.js and [name of module].js) THis way you separate all your code which makes it feasible to move your code.
You can define modules as:
(function () {
'use strict';
angular
.module('myapp.login', [
'myapp.login.controllers',
'myapp.login.services'
]);
}());
And you need to include this in the app.js, like this
angular
.module('myapp', [ 'ngRoute',
'ngResource',
'myapp.login'
])
This way you can add all your modules.
One more important folder that you would like to include is the common folder. Where you can include modules for all the reusable components that you might have in your application.
For testing you can have a separate test folder with (e2e and unit folders) inside it. I have been using cucumber for testing and I have created features folder where I create folders for each module in which I define features. Also you can create a folder named steps where you can have separate js files for each module.
On top of it you can have config file where you can create variables for all your api's so that you can control it from one place.
Hope this helps :)
Part I
This is upto you. If you don’t want to make the raw components publicly visible keep them in separate directories outside public directory.
Part II
My opinion is that you should create two resource URI - “/users” and “/tasks”. Treat them as many-to-many entities for future expansion. This way you may also have tasks assigned to multiple users. For example, “electricity bill payment”, shared between you and your partner.
To get all users issue GET request to “/users” with PARAM tasks=null. To get all users linked to a set of tasks, GET /users with PARAM tasks=<comma separated taskIds>. GET /users/{user_id} responds with user details and associated tasks. To create one or more user, POST to “/users”.
Similarly, to get all tasks issue GET request to “/tasks” with PARAM users=null. To get all tasks linked to a set of users, GET /tasks with PARAM users=<comma separated userIds>. GET /tasks/{task_id} will respond with task details and associated users. To add one or more tasks, POST to “/tasks”; optionally send users=<comma separated userIds> else assume current user on server side.
To make relations between existing tasks and users use PUT /tasks/{task_id} with PARAM users=<comma separated userIds>
Should I place them in the public folder of the Laravel installation or should I keep them completely
separate since the I would be issuing calls to the resources in the web service and it would return
JSON data and hence there is no need for them to be in the same place.
First, due to the Same Origin Policy, you should put Angularjs files in the public folder.
If you want to put it in another ip:port, then should use JSONP instead of JSON. (related)
Now, for the second part.
You're on the right path? It is a matter of opinion. Make experiments. And decide for yourself. As I see it, there is no best practices in matters of design depends on the use cases.
Either way, I think your approach is not right. You want to open the list of users? No! Another option would be to add a field to the task called "owner", and match the logged in user.
To remain RESTful, you can use a token or cookie to send the user information without a session.