Skip to content

Commit

Permalink
Add instructions outside of Laravel
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffreyWay committed Feb 25, 2014
1 parent 1abe69c commit b59e4c1
Showing 1 changed file with 33 additions and 1 deletion.
34 changes: 33 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,36 @@ then you'll access all variables, like so:

```js
MyNewNamespace.varName
```
```

### Without Laravel

If you're not using Laravel, then you'll need to hard-wire things yourself. (Or, feel free to submit a pull request with an implementation for your desired framework.)

First, create an implementation of the `Laracasts\Utilities\JavaScript\ViewBinder` interface. This class is in charge of inserting the given JavaScript into your view/page.

```php
<?php

class MyAppViewBinder implements Laracasts\Utilities\JavaScript\ViewBinder {

// $js will contain your JS-formatted variable initializations
public function bind($js)
{
// Do what you need to do to add this JavaScript to
// the appropriate place in your app.
}
}
```

Next, put it all together:

```php
$binder = new MyAppViewBinder;
$make = new PHPToJavaScriptTransformer($binder, 'window'); // change window to your desired namespace
$make->javaScript(['foo' => 'bar']);
```

Now, you can access `window.foo` from your JavaScript.

Remember, though, this is only necessary if you aren't using Laravel. If you are, then just reference the service provider, as demonstrated above.

0 comments on commit b59e4c1

Please sign in to comment.