Skip to content

Instantly share code, notes, and snippets.

@ritch
Created November 16, 2013 00:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ritch/7494351 to your computer and use it in GitHub Desktop.
Save ritch/7494351 to your computer and use it in GitHub Desktop.
Connecting Angular to MySQL through LoopBack
// angular service
app.factory('Restaurant', function($resource) {
return $resource('/api/restaurants/:id', {id: '@id'});
});
var db = app.dataSource('db', {
"connector": require('loopback-connector-mysql'),
"host": "demo.strongloop.com",
"port": 3306,
"database": "demo",
"username": "demo",
"password": "L00pBack"
});
<table class="table table-hover table-striped">
<tr ng-repeat="restaurant in restaurants">
<td>
<a href="#/menu/{{restaurant.id}}">
<img class="img-rounded pull-left" ng-src="img/restaurants/{{restaurant.shortName}}.jpg">
<b>{{restaurant.name}}</b>
</a>
<p>{{restaurant.description}}</p>
</td>
<td>
<fm-rating ng-model="$parent.restaurant.rating" readonly="true"></fm-rating>
</td>
<td>
<fm-rating ng-model="$parent.restaurant.price" symbol="$" readonly="true" ></fm-rating>
</td>
</tr>
</table>
app.controller('RestaurantsController',
function RestaurantsController(Restaurant) {
$scope.restaurants = Restaurant.query(filterAndSortRestaurants);
});
// LoopBack model
var Restaurant = app.model('restaurant', {
dataSource: 'db',
properties: {
id: {type: String, id: true, generated: true},
shortName: String,
price: Number,
rating: Number,
cuisine: String,
opens: String,
closes: String,
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment