why my $http function called twice? and how usage $resource replace them?

4,154 views
Skip to first unread message

Zoom.Quiet

unread,
Jun 18, 2013, 11:07:10 AM6/18/13
to ang...@googlegroups.com
thanx for AngularJS at all, but i'm still fresh in the brilliant world;

so my need:
- embded Angular into existent site(built by bottle)
- work with RESTful api, such as:
$ curl http://localhost:8080/api/ct/x13061418375031396CRX90/dl
{"msg": "increased", "uuid": "x13061418375031396CRX90", "echo": 7,
"method": "GET", "act": "ct/dl"}

- means summation download number,total number back echo as "echo":7

in my try demo, had finished this function:

<!doctype html>
<html lang="zh" ng-app>
<head>
</head>
<body>
<div ng-controller="restService">
<h1><a href="#"
ng-click="rest_up_dl('x13061418375031396CRX90')">download</a></h1>
<h1>total: [[theme_dl]]!</h1>
</div>

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular-resource.min.js"></script>

<script>
function restService($scope, $http) {
$scope.method = 'GET';
var apiurl = 'http://localhost:8080/api/ct/dl/'
$scope.rest_up_dl = function(uuid) {
$scope.response = null;
var uri = apiurl+uuid
$http({method: 'GET', url: uri}).
success(function(data, status) {
console.log("data:"+data);
console.log("success:"+data.echo);
$scope.status = status;
if(0==data.success){
$scope.dc = "error by srv.";
}else{
$scope.theme_dl = data.echo;
$scope.isAllFetched = true;
}
}).
error(function(data, status) {
console.log("error:"+status);
}
);
};
};

angular.bootstrap(document.documentElement,
[function($interpolateProvider){
$interpolateProvider.startSymbol('[[');
$interpolateProvider.endSymbol(']]');
}]);

</script>
</body></html>

BUT:
- as snap showed
- every time, i click the "download" link
- console log showed that $http function is run twice?!
what is happened?
where is my mistake?

AND i know $resource is recommend replace $http
- but all i fund demo is not show how to sent into page parameter , like
ng-click="rest_up_dl('x13061418375031396CRX90')"
- because the html page is generated by Python
- for the existent site, i'm no time rebuild all by Angular

thanx for all suggest...


--
人生苦短, Pythonic! 冗余不做,日子甭过!备份不做,十恶不赦!
KM keep growing environment culture which promoting organization be learning!
俺: http://about.me/zoom.quiet
许: http://creativecommons.org/licenses/by-sa/2.5/cn/
snap4ZQMBP_130618_22.40.26.png

Zoom.Quiet

unread,
Jun 19, 2013, 11:45:55 AM6/19/13
to ang...@googlegroups.com
now is becamed thrice call !
dom as:

<!DOCTYPE html>
<html lang="zh" ng-app>
...
<div class="hero-unit" ng-controller="restService">
...
<div id="ng-{{ t.uuid }}" ng-click="rest_up_dl('{{ t.uuid }}')">
</div>
<snap ng-init="rest_get_dl('{{ t.uuid }}')">
totle:[[theme_dl]]
</snap>
...
<a href="#"
ng-click="rest_up_dl('{{ t.uuid }}')"
class="btn btn-inverse btn-large">
Angular
</a>

- every time page reload, in Console print thrice call

$scope.rest_get_dl = function(uuid) {
$scope.response = null;
var uri = uri_ct+uuid
$http({method: 'GET', url: uri}).
success(function(data, status) {
console.log("/api/ct/:uuid");
console.log("success:"+data.echo);
$scope.status = status;
if(0==data.success){
$scope.dc = "error by srv.";
}else{
$scope.theme_dl = data.echo;
$scope.isAllFetched = true;
}
}).
error(function(data, status) {
console.log("error:"+status);
}
);
};

- but in networking is normal only one time call REST api server.
- BTW, for fix CORS problem
- bottle through OPTION method, permit next call on
- and in Batarang show me Scopes is chaos ...

BUT i realy mistake is what, can not understand now...
KM keep growing environment culture which promoting organization be learnning!
snap4ZQMBP_130619_23.33.20.png
snap4ZQMBP_130619_23.29.42.png
snap4ZQMBP_130619_23.30.34.png

Zoom.Quiet

unread,
Jun 20, 2013, 8:28:16 AM6/20/13
to ang...@googlegroups.com
new try , and new problem:
- now i try usage jQuery transfer click event
- dom as:
...
<a href="/path/2/file.ex"
onclick="$('#ng-{{ uuid }}').click();">
download
</a>
<span id="ng-{{ uuid }}" ng-click="rest_up_dl('{{ uuid }}')">
Angular
</span>

- the {{ uuid }} is not Angular templet, that is Jinja2 templet...

BUT, as snap:
- snap4ZQMBP_130620_20.15.18.png is direct click link "Angular" all is OK
- snap4ZQMBP_130620_20.15.27.png is click link "download" us inclick
event transfer to "Angular"
- so look like rest_up_dl() is be call
- but something is bad, make server pending the OPTIONS ask
- snap4ZQMBP_130620_20.17.00.png is in Console direct import
$('#ng-x13061417490385415CRX89').click()
- is same direct click link "Angular" all is OK

SO make me mess
- what is happened?
- why jQuery $.click() is not working ?
...
snap4ZQMBP_130620_20.15.18.png
snap4ZQMBP_130620_20.15.27.png
snap4ZQMBP_130620_20.17.00.png

xrd

unread,
Jun 20, 2013, 1:31:34 PM6/20/13
to ang...@googlegroups.com
Zoom.Quiet, you are my favorite person on the Internet. 

Can you post code to a JSFiddle or Plunkr or post your code on a server somewhere? It is difficult to debug via an email message.

Chris

Zoom.Quiet

unread,
Jun 21, 2013, 12:49:05 AM6/21/13
to ang...@googlegroups.com
On Fri, Jun 21, 2013 at 1:31 AM, xrd <xrda...@gmail.com> wrote:
> Zoom.Quiet, you are my favorite person on the Internet.
>

- realy?! thanx for all , i just one old guy in China favorite
Pythonic things ;-)

> Can you post code to a JSFiddle or Plunkr or post your code on a server
> somewhere? It is difficult to debug via an email message.
>

- i know plnkr, but can not usage good:
http://embed.plnkr.co/gb8ypY7PIsNfUNsahxtq
- same code , but can not running...

- so i had to publish as realy site:
http://sayeahoo.sinaapp.com/static/angular.html
- base SAE , same GAE ,on kinds of PaaS in China
- it's working... show all my problem...

my NEED:
- append angular action on download link <a>

my problems:
- in theory, href and click in <a> , the click event is first
- i guess gn-click is same
- but i faulted
- so i try nature click event
- base jQuery transfer click
- but i faulted
- the ogg thing is:
- same dom, in Console direct input
$('#angular4click').click()
- everything is OK

so what can i thinking about it?
KM keep growing environment culture which promoting organization be learnning!

xrd

unread,
Jun 21, 2013, 1:39:18 PM6/21/13
to ang...@googlegroups.com
Hey Zoom.Quiet.

I am not entirely sure what you want to do, but I tried to mimic your code on a sample rails application:


Code on GitHub:


Does this help you?

Chris

On Tuesday, 18 June 2013 08:07:10 UTC-7, Zoom.Quiet wrote:

Zoom.Quiet

unread,
Jun 21, 2013, 10:48:42 PM6/21/13
to ang...@googlegroups.com
On Sat, Jun 22, 2013 at 1:39 AM, xrd <xrda...@gmail.com> wrote:
> Hey Zoom.Quiet.
>
> I am not entirely sure what you want to do, but I tried to mimic your code
> on a sample rails application:
>
> http://zqdownload.herokuapp.com/
>

- WOW! that soooo welcome!
- let me learnning first, RoR Never usage
- but in fact, my core problem is mistake in html with Angular
- so base can working demo, i think everything will be OK

thanx for all !
in tech world is all smart and nice guys ...
> --
> You received this message because you are subscribed to the Google Groups
> "AngularJS" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to angular+u...@googlegroups.com.
> To post to this group, send email to ang...@googlegroups.com.
> Visit this group at http://groups.google.com/group/angular.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>



--
人生苦短, Pythonic! 冗余不做,日子甭过!备份不做,十恶不赦!
KM keep growing environment culture which promoting organization be learnning!

Zoom.Quiet

unread,
Jun 26, 2013, 9:59:34 AM6/26/13
to ang...@googlegroups.com
On Sat, Jun 22, 2013 at 10:48 AM, Zoom.Quiet <zoom....@gmail.com> wrote:
> On Sat, Jun 22, 2013 at 1:39 AM, xrd <xrda...@gmail.com> wrote:
>> Hey Zoom.Quiet.
>>
>> I am not entirely sure what you want to do, but I tried to mimic your code
>> on a sample rails application:
>>
>> http://zqdownload.herokuapp.com/
>>
> - let me learnning first, RoR Never usage
> - but in fact, my core problem is mistake in html with Angular
> - so base can working demo, i think everything will be OK
>
> thanx for all !
> in tech world is all smart and nice guys ...
>
>> Code on GitHub:
>>
>> https://github.com/xrd/zoomquiet-download/
>> https://github.com/xrd/zoomquiet-download/blob/master/app/controllers/welcome_controller.rb
>> https://github.com/xrd/zoomquiet-download/blob/master/app/assets/javascripts/download.js.coffee
>>
>> Does this help you?
>>

thanx again!
- RoR for me is toooo magic
- coffeescript is ok
- but mix with resource can not merge into my code
- at last, just usage u skill:
$window.location.href = ...
everything is perfect!-)
...
Reply all
Reply to author
Forward
0 new messages