lundi 1 février 2016

should we communicate with component via actions or data

I am confused about what is better approach, should we prefer to communicate between components, components/controllers via data or actions more?

Here is an example: I have a file uploader component (wrapping the jquery plugin) and the way I have the component setup is like this:

1) the controller that contains the file uploader component defines a property via which two entities are going to share information about the file being uploaded

//some controller template
//where uploadedFiles is a property of the context controller
{{file-uploader uploadedFiles=uploadedFiles}}

2) this property is passed to the component and the component populates it with errors/progress etc as we initiate the upload. The structure of this data object looks like:

{
   'file': file object we get from plugin
   'errors': [],
   'progress':10,
   .... 
}

This component also does some basic file validation such as file type and size. When these errors happen, we populate the data object with errors. Now I am debating - should I have an action that gets triggered every time these custom validations fails, or is it ok to have an observer in the controller to inspect the data objects and then trigger whatever action it needs to.

I am leaning more towards communicating things via data itself because it keeps the interface of communication cleaner, but I also understand it cost an overhead of observers.

Passing actions on the other hand avoids having observers but I don't fully agree with it because then we might have 10 other situations where we might feel like passing "action" is the right choice and then pollute the component code(when it can be communicated via data).

I do agree that in certain cases we must have to pass actions, but given the choice that things can be communicated via bound data, is action the better choice?

Thanks.




Aucun commentaire:

Enregistrer un commentaire