admin管理员组文章数量:1221288
I would like to use Angular.js in my Sinatra applications. Unfortunately, I couldn't find any useful tips on this. I did find some Rails examples, however I have always found Rails and Padrino rather difficult to deal with, compared to the minimalistic philosophy of Sinatra.
I watched a number of videos (found by Googling angular.js), but am still finding it difficult to apply to Sinatra.
The most comprehensive tutorial I found so far was one from yearofmoo.
But still I am lost trying to apply this to Sinatra, and hacking my way out of this appears not to be an option as a simple error anywhere might set me off the right path anyway. I am lost and I admit it!!
Kindly any help based on your experience of trying to do something similar would be much appreciated, if shared. All I need at this point is to path my JSON from a Sinatra app to angular.js powered pages.
Thanks.
I would like to use Angular.js in my Sinatra applications. Unfortunately, I couldn't find any useful tips on this. I did find some Rails examples, however I have always found Rails and Padrino rather difficult to deal with, compared to the minimalistic philosophy of Sinatra.
I watched a number of videos (found by Googling angular.js), but am still finding it difficult to apply to Sinatra.
The most comprehensive tutorial I found so far was one from yearofmoo.com.
But still I am lost trying to apply this to Sinatra, and hacking my way out of this appears not to be an option as a simple error anywhere might set me off the right path anyway. I am lost and I admit it!!
Kindly any help based on your experience of trying to do something similar would be much appreciated, if shared. All I need at this point is to path my JSON from a Sinatra app to angular.js powered pages.
Thanks.
Share Improve this question edited Jan 25, 2013 at 3:48 Perception 80.6k19 gold badges188 silver badges196 bronze badges asked Jan 24, 2013 at 21:20 zotherstupidguyzotherstupidguy 3,02410 gold badges41 silver badges59 bronze badges 7 | Show 2 more comments2 Answers
Reset to default 13As I stated in the comments above, the application structure would no longer rely on the server for templating the UI or generation of markup. Your server would essentially be a data and file host.
Okay.. presuming you have some route in Sinatra set up to return the following json (with content-type: application/json):
[
{ "id": 1, "name": "Foo" },
{ "id": 2, "name": "Bar" }
]
You would then use something like this in Angular to load that data (basically):
app.js
//create your application module.
var app = angular.module('myApp', []);
//add a controller to it
app.controller('MyCtrl', function($scope, $http) {
//a scope function to load the data.
$scope.loadData = function () {
$http.get('/Your/Sinatra/Route').success(function(data) {
$scope.items = data;
});
};
});
Then in your markup you'd do this:
<!DOCTYPE html>
<html ng-app="myApp">
<head>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.0.3/angular.min.js"></script>
<script src="app.js"></script>
</head>
<body ng-controller="MyCtrl">
<button ng-click="loadData()">Load Data From Server</button>
<ul>
<li ng-repeat="item in items">ID: {{item.id}}, Name: {{item.name}}</li>
</ul>
</body>
</html>
I hope that helps.
I found this Sinatra tutorial was helpful even though it uses Knockout.js not Angular. It helps you build a Sinatra application that returns JSON, and it was quite straightforward to take the concepts and code from the Angular tutorial to connect to this simple backend.
本文标签: javascriptAngularjs with SinatraStack Overflow
版权声明:本文标题:javascript - Angular.js with Sinatra - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1739345552a2159144.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
$http.get('/MySinatra/Route').success(function(data) { /*do whatever*/ });
– Ben Lesh Commented Jan 24, 2013 at 21:48