Ember JS : Creating nested urls for relationships REST-style

I’ve been working on a project in Ember JS with the Ember-client extension for a while now. Ember is very frustrating to work with because of the lack of documentation.

I was recently looking for a way to have Ember use urls for loading and saving resources from a REST-api using the same conventions used by api’s like the ones Facebook and Twitter use.

For example, requesting the comments for a post would have an endpoint like this:

/posts/{ID}/comments

Unfortunately Ember will just post the user id to this endpoint:

/comments

There is a way to have Ember use /posts/{ID}/comments as endpoint but that requires the payload from the api for a post to contain the property ‘links’, containing the desired endpoint. To me this seems undesirable; endpoints should be derived from conventions. Besides, if you have no control over the api then you have no control over this ‘links’ property

You could add the ‘links’ property to the payload by overriding Ember’s RESTAdapter or RESTSerializer but that is a difficult place to do that.

I analysed what Ember does in the background when it finds the ‘links’ property on a payload : it creates relationships in the corresponding model. I managed to create an override that changes the ‘link’ property of the corresponding relationship.

This is my code:

I’m not really happy with this solution because manually changing the ‘link’ property after ember created the relationship is far from ideal. But in the mean time, untill I find a better approach I’m sticking to this in order to get my application to work.

 

Concrete5.7 : Javascript validation for a block’s form

If you are developing a block for Concrete 5.7 you might be looking for the Javascript function ‘ccmValidateBlockForm’ that handles the clientside validation of your block’s form. It seems to have dissappeared in Concrete 5.7 which leaves you with no way to prevent the form from being submitted if it contains validation errors.

I ran into this problem recently as I was developing my own block. The wrong approach is to listen to a form submit event. I tried this but discovered it submitted anyway as Concrete was triggering a submit with Javascript. The trick is to prevent that script from being triggered. Concrete listens to a click on the form’s submit button which is in fact not a submit button but an anchor using Javascript to trigger the click event on the real submit button which is hidden from sight.

This is how to solve it:

 

Replace ‘myFormValidationFunction’ with the name of your function that validates the form. Return false to prevent the form from submitting or true to allow it to submit.

Concrete 5.7 : Creating a single page for the frontend

In this post I would like to show you how to create a single page in your package for the frontend of a Concrete 5.7 website. In this tutorial we will call that package ‘YourPackage’ but of course that should be whatever your package is called.

Let’s say you want to create a single page where visitors can sign up for a newsletter. A single page is composed of a template and a controller (The controller is optional).

We will call our controller ‘SubscribeTo’. Following C5’s naming conventions this implies that :

    • The filename of the template will be called ‘subscribe_to.php’.
    • The filename of the controller will be called ‘subscribe_to.php’.

Your directory structure will look like this:

For the controller :

/packages/your_package/controllers/single_pages/subscribe_to.php

For the template :

/packages/your_package/single_pages/subscribe_to.php

And this is what your controller file will look like:

Now let’s say you want to install the single page when your package get’s installed. We will add a method to the package controller in order to do so. (The package controller is  :  /packages/your_package/controller.php).

Now the single page gets installed when you install your package. In this example I added the method ‘addSinglePage’ to the package controller but if you want to follow programming conventions it is better to create a separate class with this method (like a service/util). This way you delegate the logic. That makes it easier to refactor and you can call the method from other packages or code you create.

 

Code Examples

I’ve just started off recently (jan 10th, 2015) so there’s not much to see here yet. I’ve set up this blog mainly to post interesting examples and tutorials on code cause unfortunately there’s never enough documentation out there.

I develop a lot of websites and applications in PHP and Javascript and I would like to share some cool stuff with you guys in the years to come.

Stay tuned!