Backend Ajax protocol: Difference between revisions
From LimeSurvey Manual
Line 44: | Line 44: | ||
* How much of the CRUD will happen on client? All of it. REST is used to sync client to server. | * How much of the CRUD will happen on client? All of it. REST is used to sync client to server. | ||
* How much would backbone replace Yii and things generated with Gii? | * How much would backbone replace Yii and things generated with Gii? | ||
* Would all views be put on the client? I'd prefer if we didn't split views between server and client. Here using Mustache would be a clear advantage, since it easily can be used from both Yii and Backbone. [[User:Olle|Olle]] ([[User talk:Olle|talk]]) 20:43, 28 September 2016 (CEST) | * Would all views be put on the client? I'd prefer if we didn't split views between server and client. Here using Mustache would be a clear advantage, since it easily can be used from both Yii and Backbone. Or we can configure Twig to look like Underscore templates. [[User:Olle|Olle]] ([[User talk:Olle|talk]]) 20:43, 28 September 2016 (CEST) | ||
== Learning material == | == Learning material == |
Revision as of 21:02, 28 September 2016
Discussion
There should be a standard JSON protocol between the LS backend and Ajax calls to check for errors, permissions and if the user still is logged in. A suggestion would be to wrap $.ajax
in a function that checks this for you, like LS.ajax
.
All server Ajax calls must return a JSON in the following format:
{
type: int,
result: mixed,
error: {code: int, message: string},
loggedIn: boolean,
hasPermission: boolean,
}
Of course there needs to be server side support for this protocol, too, with helper functions to output correct JSON code, e.g. Ajax::outputResult
or Ajax::outputError($code, $message)
.
Preferably, the function should be able to show an error if server throws 500.
Message types
Type | Fields | Description |
---|---|---|
notloggedin | - | User is not logged in; the login form should appear in a modal and then continue with the action |
nopermission | - | No permission |
success | success : string | Show success message |
error | error : string | Show error message |
custom | result : mixed | Custom action, meaning a supplied Javascript function will take care of the response from the server, e.g. feeding HTML to a modal body. BUT: Maybe we could have a message type "modal" which sends modal title, body and footer? |
Backbone
Since Javascript library Backbone might be used in the future, it would be nice to know how they handle stuff like this.
Some questions:
- Would we need to duplicate the model code in both backend and frontend? Yes, we would need to code validation in both Backbone model and Yii model.
- How much of the CRUD will happen on client? All of it. REST is used to sync client to server.
- How much would backbone replace Yii and things generated with Gii?
- Would all views be put on the client? I'd prefer if we didn't split views between server and client. Here using Mustache would be a clear advantage, since it easily can be used from both Yii and Backbone. Or we can configure Twig to look like Underscore templates. Olle (talk) 20:43, 28 September 2016 (CEST)
Learning material
Implementation
A first prototype of the backend classes can be seen here. Instead of a helper, this class can actually be put into LSYiiController, making it available to all controllers in LS.
Example:
public function indexAction()
{
$this->ajax->outputSucces(gT('Success!'));
}