Skip to content
This repository has been archived by the owner on Jul 22, 2022. It is now read-only.

Commit

Permalink
add requirejs support of ssa generator
Browse files Browse the repository at this point in the history
  • Loading branch information
deblockt committed Nov 24, 2014
1 parent 9dfeeed commit 9e0e617
Show file tree
Hide file tree
Showing 7 changed files with 197 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ vendor/
cache/
**/cache/
nbProject/
composer.phar

#############
## Windows detritus
Expand Down
36 changes: 36 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,42 @@ FileService.upload(document.getElementById('simpleFileUploadInput').files)
});
```


Ssa support multiple javascript framework :
- you can use ssa standolone only on include ssa.js file
- you can use ssa with angular js you just need to include ssa.js file and your service generated file. After use injection dependencies for get your service.
for exemple :
```javascript
// add ssa as module dependencies
var controller = angular.module('ssa.test', ['ssa']);
// get simply your service on your controller with the service name. here the service is helloWorldService
controller.controller('controller', function($scope, helloWorldService){
});
```
- you can use ssa with requirejs you just need to include ssa on your configuration of requirejs.
```javascript
// configuration must containe a ssa link
require.config({
paths: {
// you must add ssa srcipt on your configuration
"ssa": "path/to/ssa/javascript/file",
// warning if you don't use htaccess service param is like this /serviceName
// if you use htacess you can have url like this /javascript/ssa/service/servicename.js who redirect on javascript.php
// path of your javscript service generator
"ssaService" : "javascript.php?type=requirejs&service="
}
});

// juste require your service
require( ["ssaService/helloWorldService"],
function(helloWorldService) {
// helloWorldService is you php service
}
);
```

If you want see ssa exemple you can look on test/ssa/toEndTest directory

### Configuration

The ssa configuration is different between standalone version, and the symfony version.
Expand Down
6 changes: 6 additions & 0 deletions javascript/ssa.js
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,12 @@ if (typeof angular != 'undefined') {
});
}

if (typeof define != 'undefined') {
define([], function(){
return ssa;
});
}


/** redefinition array.isArray */
(function () {
Expand Down
4 changes: 2 additions & 2 deletions src/ssa/converter/AngularJavascriptConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class AngularJavascriptConverter extends JavascriptConverter {
*/
protected function convertClass($className) {
$txt = 'var ssaModule = angular.module(\'ssa\');'.$this->END_OF_LINE;
$txt .= 'ssaModule.factory(\''.$className.'\',function(ssa){'.$this->END_OF_LINE;
$txt .= 'ssaModule.factory(\''.$className.'\',[\'ssa\', function(ssa){'.$this->END_OF_LINE;
$txt .= parent::convertClass($className);
return $txt;
}
Expand All @@ -30,6 +30,6 @@ protected function convertClass($className) {
* @return string
*/
protected function endConvertClass($className) {
return 'return ' . $className. ';'. $this->END_OF_LINE .'});';
return 'return ' . $className. ';'. $this->END_OF_LINE .'}]);';
}
}
35 changes: 35 additions & 0 deletions src/ssa/converter/RequireJsJavascriptConverter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace ssa\converter;

use ssa\converter\JavascriptConverter;

/**
* javascript converter for require js support
* Warning SSA must be defined on require js config
*
* @author Thomas Deblock
*/
class RequireJsJavascriptConverter extends JavascriptConverter {
/**
* create require js define
*
* @param type $className
* @return type
*/
protected function convertClass($className) {
$txt = 'define([\'ssa\'], function(ssa){'.$this->END_OF_LINE;
$txt .= parent::convertClass($className);

return $txt;
}

/**
* end the requirejs module
* @param type $className
* @return string
*/
protected function endConvertClass($className) {
return 'return ' . $className. ';'. $this->END_OF_LINE .'});';
}
}
11 changes: 10 additions & 1 deletion test/ssa/toEndTest/javascript.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,22 @@
include 'serviceConfig.php';

use ssa\converter\AngularJavascriptConverter;
use ssa\converter\RequireJsJavascriptConverter;
use ssa\converter\JavascriptConverter;
use ssa\converter\SimpleUrlFactory;

$url = substr($_SERVER['REQUEST_URI'],0, strrpos($_SERVER['REQUEST_URI'], '/'));
// get base url for generic javascript generator
// if you know your base url you can remove three next lines
$serverRequestURI = $_SERVER['REQUEST_URI'];
$startUrl = substr($serverRequestURI, 0, strrpos($serverRequestURI, '?'));
$url = substr($startUrl,0, strrpos($startUrl, '/'));

// create an url factory used for call webservice
$factory = new SimpleUrlFactory("http://$_SERVER[HTTP_HOST]$url/run.php?service={action}&test=true");
if (isset($_GET['type']) && $_GET['type'] === 'angular') {
$converter = new AngularJavascriptConverter($_GET['service'], $factory);
} else if (isset($_GET['type']) && $_GET['type'] === 'requirejs') {
$converter = new RequireJsJavascriptConverter(trim($_GET['service'], '/'), $factory);
} else {
$converter = new JavascriptConverter($_GET['service'], $factory);
}
Expand Down
107 changes: 107 additions & 0 deletions test/ssa/toEndTest/test_requirejs.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
<head>
<title> Page de test de ssa</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<script type="text/javascript" src="http://code.jquery.com/jquery-1.11.1.min.js"></script>

<script type="text/javascript" src="http://requirejs.org/docs/release/2.1.15/minified/require.js" ></script>
<script type="text/javascript">
// configuration must containe a ssa link
require.config({
paths: {
// you must add ssa srcipt on your configuration
"ssa": "../../../javascript/ssa",
// warning if you don't use htaccess service param is like this /serviceName
// if you use htacess you can have url like this /javascript/ssa/service/servicename.js who redirect on javascript.php
"ssaService" : "javascript.php?type=requirejs&service="
}
});

require( ["ssaService/helloWorldService"],
function(helloWorldService) {
helloWorldService.helloYou('deblockt').done(function(data) {
$('#helloYou').html(data);
});

$().ready(function(){
$('input[name="your_name"]').blur(function(){
helloWorldService.helloYou($(this).val()).done(function(data){
$('helloYou2').html(data);
});
});
});
}
);
</script>
</head>
<body>
<div>Hello world return : <span id="helloYou"></span> </div>
<pre>
<code class="javascript">
HelloWorld.helloYou('deblockt').done(function(data) {
document.getElementById('helloYou').innerHTML = data;
});
</code>
</pre>
<div> Hello You <input name="your_name" placeholder="Your name"/> : <span id="helloYou2"></span></div>
<pre>
<code class="javascript">
$('input[name="your_name"]').blur(function(){
HelloWorld.helloYou($(this).val()).done(function(data){
document.getElementById('helloYou2').innerHTML = data;
});
});
</code>
</pre>

<form action="http://localhost:8080/ssa/test/ssa/toEndTest/run.php" method="POST" enctype="multipart/form-data">
<input type="text" name="service" value="HelloWorld.getFileContent"/>

<input type="file" multiple="multiple" id="fileUpload1" name="file[]"/>
<input type="file" multiple="multiple" id="fileUpload2" name="file[]"/>
</form>




<h3> complete source code </h3>

<pre>
<code class="javascript">
// configuration must containe a ssa link
require.config({
paths: {
// you must add ssa srcipt on your configuration
"ssa": "../../../javascript/ssa",
// warning if you don't use htaccess service param is like this /serviceName
// if you use htacess you can have url like this /javascript/ssa/service/servicename.js who redirect on javascript.php
"ssaService" : "javascript.php?type=requirejs&service="
}
});

require( ["ssaService/helloWorldService"],
function(helloWorldService) {
helloWorldService.helloYou('deblockt').done(function(data) {
$('#helloYou').html(data);
});

$().ready(function(){
$('input[name="your_name"]').blur(function(){
helloWorldService.helloYou($(this).val()).done(function(data){
$('helloYou2').html(data);
});
});
});
}
);
</code>
</pre>
</body>
</html>

0 comments on commit 9e0e617

Please sign in to comment.