Monday, August 3, 2015

Using Method Call In Meteor

In my last post I showed how to use publish/subscribe in meteor and how to allow access for specific  data in our database. But still every user have Create/Update/Delete privileges. Now we should remove those client side privilege and do those operations only in server side. To do that we can use meteor methods. The all the database functionalities can be execute only in server side.
First we will remove insecure package from meteor.
Run the application and try to create a student. You will get Access Denied Error.
Methods allow us to call database create/update/delete operation without executing that code in client side. First we will implement method for create student. Add studentMethods.js file into server folder and content should be like below.
Then modify studentCreate.js file like below. Then student create should work fine.
Download source code from github.

Meteor Publish and Subscribe

In my last post I showed how to access route parameters in meteor application. In this post I will explain how to implement publish/subscribe in meteor application.
For development purposes meteor has added several packages which we should remove before final implementations. One package is autopublish. In our current application, though we show/hide details depending on logged in users, any user can access all data using browser console. 
But for users collections, which is provided by accounts package, will only show data related to current user. Because internally accounts package handle security for us.
Allow access for all data impact for security and also for performance. But we can remove those by removing autopublish package and allow access to those collections in a different way.
First we will remove that package.
Then add publications.js file into server folder and content should be like below.
Then we need to subscribe for Students collection. Every template does not need Students collections. So, we should subscribe for Students collection in an appropriate place while considering performance. So we will put that to routes which need students collection.
But we should think what will happen when we deploy into to remote server and access it. It will take some time to load student data for each client. Therefore it's better if we add a loading content. Add loading.html file like below.
Then in routes.js add loadingTemplate like below.
Then modify students and studentDetails routes like below.
Download source code from github.

Access Route Parameters from Meteor

Every web application need to access URL parameters. In meteor, using routes, we need to access URL parameters. This post will continue with the code implemented in my last post.
First we will add StudentDetails.html file which has content like below.
In the routes.js add new route for studentDetails.
Then we will modify studentList.html file.
Run the application now. Then you will see students details page like below.
Download source code from github.