When you want to retrieve the key value in an input element that has a name array you need to do the following:
1 | <input type="text" name="tag[1]" value="blah"/> |
1 | var tagSelect = []; |
Hope it helps you out.
When you want to retrieve the key value in an input element that has a name array you need to do the following:
1 | <input type="text" name="tag[1]" value="blah"/> |
1 | var tagSelect = []; |
Hope it helps you out.
I first started using the devbridge autocomplete since it look pretty good and then I needed a tagging plugin called tagedit.
What I found was that tagedit was using jquery-ui autocomplete and it would not work at all when devbridgeautocomplete was called above.
I tried to alias it like other people said but it didn’t help.
So in the end I removed devbridge autocomplete and used jquery-ui autocomplete
Hope it helps
When you need to calculate the distance between 2 gps cordinates(latitude, longitude) you can use this mysql query.
1 | 'SELECT (((acos(sin((' + latitude + |
You can add a having statement or distance conditional to filter out rows.
Hope this helps.
This snippet will help you format a 24 hour time to a 12 hour time.
1 | function fixHour(e){ |
Hope it helps.
When you need to calculate the distance between 2 gps cordinates(latitude, longitude) you can use this function.
1 |
|
There are many variation of this function on the internet but mine returns the answer in kilometers.
Next post I’ll show how to calculate the distance between GPS in the back end.
Hope this helps.
When trying to authenticate mobile application with laravel back end server, you cannot use the default session you would use in php. The common practice for authenticating is using json web tokens.
##Steps:
1.Depending on which version of laravel you are using install the correct plugin for laravel cors with composer.
I’m using laravel 4 so I used:1
composer require barryvdh/laravel-cors:0.2.x
2.Next I created the config file with1
php artisan config:publish barryvdh/laravel-cors
3.Next I modified the config file in app/config/packages/barryvdh/laravel-cors/config.php1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18'defaults' => array(
'supportsCredentials' => false,
'allowedOrigins' => array(),
'allowedHeaders' => array(),
'allowedMethods' => array(),
'exposedHeaders' => array(),
'maxAge' => 0,
'hosts' => array(),
),
'paths' => array(
'api/v1/*' => array(
'allowedOrigins' => array('*'),
'allowedHeaders' => array('*'),
'allowedMethods' => array('*'),
'maxAge' => 3600,
)
)
You can use whatever route you want, I just used /api/v1.
4.Next install jwt plugin. Depending on your version of laravel, install a different version of the plugin with composer.1
composer require tymon/jwt-auth:0.4.*
5.Next I created the config file with1
php artisan config:publish tymon/jwt-auth
You can change the jwt settings here if you want.
6.Next you will create a filter to check for jwt token before processing a route. I put the filter in my route.php file.1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17Route::filter('authMobile', function($route, $request)
{
try{
$token = JWTAuth::getToken();
$user = JWTAuth::toUser($token);
$tokenStr = JWTAuth::getToken()->__toString();
if ($user->loginToken != $tokenStr){
throw new Exception("Login Token don't match");
}
Session::put('user',$user->id);
}catch(Exception $e){
return Response::json(array(
'error' => true,
'message' => 'Invalid Session'
));
}
});
7.So an example route will look like the following:1
2
3
4Route::get('/api/v1/user', array(
'before' => 'authMobile',
'uses' =>'UsersController@getUserProfile'
));
To access the user in the action, you can retrieve it from the session.
1 | //validation happens jere |
9.Now everytime the mobile app makes a request to the server, they must add a header1
Authorization bearer Y2E1YTc2MmFkMWU4NjJmZTBiZDU1NmEzMmJhZjBmYjc3MmNkYzBiYjE3YWM5MTkxNDg2Zg...
10.Make sure your .htaccess file allows HTTP authorization.1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
Hope this helps you out.
##My Goal:
Create/Find a tinymce version 4 plugin to upload images to the server, save this image to the database and place this image in my tinymce editor
This plugin must also be able to view a list of media files(image/video) that are in my database in a gallery format where the user can click an image and place it into tinymce
Some plugins I looked into are:
After much distress, I created my own plugin.
Hopefully this will save you countless hours trying to figure out how to make an image uploader in tinymce 4.
Steps
Download the latest tinymce.js file from their site.
Define the textarea/element you want to run tinymce on
1 | <textarea name='text' rows='2' id='editor' class='form-control'></textarea> |
Add the following to your javascript file:
1 | $(document).ready(function() { |
I called my plugin imageuploader and I removed all unnecessary plugins. All plugins in the toolbar or menu bar must
be in plugins.
4.Create the tinymce version 4 plugin and place it in the plugins folder inside tinymce. Call it plugin.min.js.1
2
3
4
5
6
7/js
/js/<vendor>
/js/<vendor>/tinymce4
/js/<vendor>/tinymce4/js
/js/<vendor>/tinymce4/js/tinymce
/js/<vendor>/tinymce4/js/tinymce/plugins/imageuploader
/js/<vendor>/tinymce4/js/tinymce/plugins/imageuploader/plugin.min.js
plugin.min.js
1 | tinymce.PluginManager.add('imageuploader', function(editor, url) { |
I’ll explain step by step.1
tinymce.PluginManager.add('imageuploader', function(editor, url) {
This line creates the plugin in tinymce
1 | editor.addButton('imageuploader', {options},{params}); |
This line creates a button in the toolbar where you pass in options and parameters to iframe you will open.
Some options:
1 | title: 'Select Media', |
Title is the title of the iframe
1 | url: '/article/upload', |
url is the route of the page the iframe opens.
1 | buttons: [{ |
Created 2 buttons below the iframe. when either button is clicked, the iframe will close. However, you can bind submit to a function when clicked.
1 | onsubmit: function(e) { |
The method I used to retrieve the url from the iframe is on line 2. After you uploaded your images/video, you would
save it to a hidden input field which you will retrieve when submit button is clicked.
At this point, you can call editor.insertContent() to add it to the tinymce editor.
5.Implementing the iframe page /article/upload
On this route I created 4 screens:
###Page to show 2 buttons1
2
3
4
5
6
7
8
9
10
11<div class="col-md-12 main-button-container">
<div class="well center-block first-upload-container">
<button type="button" class="btn btn-success btn-lg btn-block" id="upload-new">
Upload New Media
</button>
<button type="button" class="btn btn-info btn-lg btn-block" id="select-media">
Select Media
</button>
</div>
</div>
<input type="hidden" name="media-url" id="media-url" data-media-type="image" data-caption=""/>
###Page to upload new media1
2
3
4
5
6
7
8
9
10
11<div class="col-md-12 upload-new-container">
<h3>Upload New Media File</h3>
<p>Media File</p>
<input type="file" name="mediafile-new" id="mediafile-new" />
<br/>
<p>Caption</p>
<input type="text" name="caption-new" id="caption-new" class="form-control"/>
<br/>
<button class="btn btn-block btn-success" id="upload-new-media">Upload</button>
<button class="btn btn-block btn-error" id="upload-new-cancel">Cancel</button>
</div>
###Page to select old media1
2
3
4
5
6
7
8
9
10
11<div class="col-md-12 select-media-viewer well center-block">
<h3 class="media-viewer-title">Media On File</h3>
<p class="media-viewer-subtext">
Please click the media you want to add to your article. Then Click use media</p>
<br/>
<div class="gallery">
</div>
<button class="btn btn-block btn-info use-media"> Use Media</button>
<button class="btn btn-block btn-error" id="use-media-cancel">Cancel</button>
</div>
I am dynamically filling gallery div with media I have and displaying them with gridify.js
###Upload file to server1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16var data = new FormData();
data.append('filename', $('#mediafile-new').prop('files')[0]);
$.ajax({
type: 'POST',
processData: false, // important
contentType: false, // important
data: data,
url: '/api/v1/upload',
dataType : 'text',
success: function(jsonData){
//do something
},
error: function(){
//do something
}
});
Hope this helps you out.
Its very fustrating when you have to create elements on the fly using the json data you got from the server.
The javascript gets very bulky and messy.
To solve this you could use handlebars.js. Handlebars.js is a templating library to help you preload templates
that you can pass in information and use.
Steps:
1.Download handlebars.js from their site.
2.Depending on which view engine you use, you have two options:
1 | <script id="some-template-tpl" type="text/handlebars"> |
3.You will need to compile the template and input the data into the template.
loading template in using ajax
1 | $.ajax({ |
loading template from script tag
1 | var template = Handlebars.compile($('#some-template-tpl').html()); |
Hope this helps.
I always forget how to bind events to elements when dynamically loaded when I need it most.
So I decided to make a blog post so I can get the answer really fast.
1 | $(document).on('click', '.class', function(ev){ |
I always forget how to target select box that has been selected when I need it most.
So I decided to make a blog post so I can get the answer really fast.
1 | //find out selected value |
Hope it helps.