Here is my code:
function MainCtrl($scope) {
$scope.thisarray = globalObj;
$scope.loadthis = function (index) {
return thisarray[index];
}
}
Here is the markup:
<table ng-controller="MainCtrl">
<tr>
<td ng-repeat="element in thisarray">
{{loadthis($index)}}
</td>
</tr>
</table>
I'm loading some data through a SOAP callback:
SOAPCLient.invoke(url, methodName, pl, true, function(obj) {globalObj = obj;
**angular.bootstrap("html");**//here is where I try to reinitialize the app
});
If you look at the SOAP call, I use bootstrap to reinitialize the app, but it has not been working. loadthis does not display any data inside the HTML table. Can anyone help me here?
You could listen for changes to the SOAP using $.watch and then $.apply when trying to force a push. See examples of how to use here
You should not be re-initializing AngularJS application every time you've got data arriving from an external source. Instead of destroying and re-creating AngularJS the way to go would be to use $apply method on a scope object.
The pseudo-code (in your controller) would be like:
SOAPCLient.invoke(url, methodName, pl, true, function(obj) {
$scope.apply(function(){
$scope.thisarray = obj;
});
});
Also, if I understand your code correctly you would invoke a SOAP call for each item in a repeater (ng-repeat) while your SOAP call is bringing back data for all objects. So the better approach would be to do a SOAP call (as in a snippet above), assign data to a variable in a scope and let the ngRepeat do its magic.
I might be misunderstanding what you are trying to achieve here so having a jsFiddle / plunker with your code would help to provide more detailed response.
Related
I'm building a small sails.js + angular.js app.
Here is a fiddle that roughly shows what my code looks like: http://jsfiddle.net/WEk3F/
index: function(req, res, next) {
Food.find({}, function foundFoods(err, foods) {
if (err) return next(err);
var data = {
name1: "test1",
name2: "test2"
}
res.view(
'food/index', {
foods: data
});
});
},
<div ng-app="myApp">
<div ng-controller="FoodController">
<ul>
<li ng-repeat="food in foods">
{{food.name}}
</li>
</ul>
<form>
<label for="name">Name:</label>
<input name="name" ng-model="editableFood.name" />
</form>
</div>
</div>
My problem is, that whenever i try to retrieve the data from my controller, i don't get those 2 items but instead it renders more and more items and just doesn't stop. even the page gets slow and unresponsive and almost freezes.
When i say
$scope.foods = [{"name": "test1"},{"name": "test2"}];
instead of
$scope.foods = Food.query();
it works. but i want the data to be coming from the backend via the controller.
the other methods (add, update etc) of the angular.js $resource module work fine for me.
/food maps to the index action of my FoodController and just returns some fixed test data
i found out what the problem was.
the angular.js $resource should only be used in a restful way, so the GET request to my food/index should return an array of objects.
in my case this wasn't the case. instead i used the index action of my controller to render a view and the result was therefor html.
so my options are to define a new route, that the $resource takes for the Query() command or define a new action that i use for the view/html stuff and use the index action again for pure restful response.
thx #sam hunter for pointing me to the right direction
the infinite loop that i received is explained here: One dimensional array of strings being parsed to 2d by angular resource
i love stack overflow
Hi guys i have a problem.
Im making a callback from an aspxgridview. I do callback when i make RowDblClick and then i switch to another tab of the pageControl and fill some controls with database information. One of those controls is another gridview. So what i want to do is when i rowdblclick in the new gridview, make another callback.
I dont know why but when i double click the new aspxgridview, my application stacks. So i haven't response of the callback. I stopped the first callback of the first grid, and the second callback works fine.
So my question is, how can i make a callback in an aspxgridview by a rowdblclick, and after that make another callback with another rowdblclick in another aspxgridview?
this is first aspxgridview
<dx:ASPxGridView ID="grillaInformes" runat="server" KeyFieldName="ID" width="100%" EnableCallBacks="false" ClientInstanceName="grillaInformes" OnCustomCallback="grillaInformes_CustomCallback">
<ClientSideEvents RowDblClick="function(s, e) { s.PerformCallback(e.visibleIndex); }"/>
and this is the second aspxgridview, whose callback doesn't work because i made another callback before of grillaInformes
<dx:ASPxGridView ID="grillaSubInformes" runat="server" KeyFieldName="ID" Width="100%" EnableCallBacks="false" ClientInstanceName="grillaSubInformes" OnCustomCallback="grillaSubInformes_CustomCallback" >
<ClientSideEvents RowDblClick="function(s, e) { s.PerformCallback(e.visibleIndex); }"/>
Have you tried calling a defined JavaScript function for the CallBacks, and stepping through the code there? For example:
function grillaInformes_RowDblClick(s, e) {
s.PerformCallback(e.visibleIndex);
}
function grillaSubInformes_RowDblClick(s, e) {
s.PerformCallback(e.visibleIndex);
}
and use the following for your grid:
<dx:ASPxGridView ID="grillaInformes" runat="server" KeyFieldName="ID" width="100%" EnableCallBacks="false" ClientInstanceName="grillaInformes" OnCustomCallback="grillaInformes_CustomCallback">
<ClientSideEvents RowDblClick="grillaInformes_RowDblClick"/>
<dx:ASPxGridView ID="grillaSubInformes" runat="server" KeyFieldName="ID" Width="100%" EnableCallBacks="false" ClientInstanceName="grillaSubInformes" OnCustomCallback="grillaSubInformes_CustomCallback" >
<ClientSideEvents RowDblClick="grillaSubInformes_RowDblClick"/>
You should see right away whether the second double-click is even starting, then step through the code behind. We do things like this often though, and have no problem with multiple callbacks running at one time.
Hi I have the following scenario.
Some elements are nested within a with
<!-- ko with: model.selected_item -->
<tr>
<td style="width:20%">Name:</td>
<td style="width:80%" class="field" data-bind="text: name"></td>
<td style="width:10px"><div class="btn_edit"></div></td>
</tr>
<tr>
etc...
<!-- /ko -->
$("div.btn_edit", component.context).on("vclick", function(e){
//edit it
}
The problem I have is that if the selected_item changes - I loose the bindings on the edit button.
In this scenario, it is a little difficult to subscribe to the model.selected_item and reapply the bindings - so I'd like to get notified when the elements within the with directive are updated and reapply then.
Is there a knockout specific way to achieve this?
An help much appreciated.
The reason you are getting this problem is that the click handler element is being destroyed by the template engine each time you update item.
Here is an example of the way to achieve what you want without resorting to jquery delegate bindings, which while they do work they are poluting your otherwise nice KO model with needless dom operations. Everytime I find myself using the old $() selector with a KO app I have to seriously consider whether there is a better model oriented way.
http://jsfiddle.net/madcapnmckay/EFQ9S/
The gist of this is to convert your model into true js classes and use those to bind KO click events rather than jquery click handlers. The beauty of this approach is that KO will rebind for you elements when they get destroyed.
var item = function (config) {
var self = this;
this.name = ko.observable(config.name);
this.edit = function () {
$("body").append("<div>lets edit " + self.name()+"</div>");
};
};
var model = function() {
this.item = ko.observable();
this.pushNew = function () {
this.item(new item({name: "new name"}));
};
}
$(document).ready(function(e) {
var mymodel = new model();
ko.applyBindings(mymodel);
mymodel.item(new item({ name: "the_name" }));
})
Hope this helps.
How can I convert a JavaScript DOM object to a jQuery object?
<tr onclick="changeStatus(this)">
function changeStatus(myObject) {
XXX.removeClass();
}
What should I write in place of XXX?
I know I can make a workaround using an id and an id selector, but it is not so elegant.
Is there any way to convert a js DOM object to a jQuery object or using the this keyword in jQuery?
var $this = $(myObject);
$this is a jQuery object. You can create jQuery objects from DOM elements.
<tr onclick="changeStatus(this)">
function changeStatus(myObject) {
$(myObject).removeClass();
}
I would like to recommend doing your event binding with jQuery as well:
<tr class="change-status">
$('.change-status').on('click', function () {
$(this).removeClass( ... );
});
This is nice because now all the JS code is in one place and can be updated (in my opinion) more easily. Note that the class I added to the <tr> element is not necessary if you want to bind to all <tr> elements in the DOM.
Simply wrap the pass the DOM object in as the first argument.
$(myObject).removeClass('foo');
I have the script below. I am trying to POST the data and insert it into a database, the jQuery executes just fine, but does not post anything, the action is working properly because when i post the data without the script, the data posts fine and is inserted into the database fine without any errors, so it seems as if the jquery function is posting nothing. can someone please help?
$('#form').live('submit',function(){
$('#form').fadeOut('slow');
$('#div').append("<h2>submittes</h2>");
return false;
});
<form id="form" method="post" action="execute.php" name="form">
<textarea id="text" name="update"></textarea>
<br>
<input type="submit" value="update" id="update-submit">
</form>
EDIT:
$('#form').live('submit',function(){
var updateTextArea = $('#textarea').val();
$.ajax({
type: "POST",
url: "execute.php",
data: updateTextArea,
success: function() {
$('#form').fadeOut('slow');
$('#div').append("<h2>updated</h2>");
}
});
return false;
});
this is what i have for the ajax, but i am still not having any success.
You don't have any AJAX calls in your javascript. You're just fading out the form, appending an h2, and preventing the default action from occurring (which would be to submit the form normally).
Here's a basic example of how to create a POST ajax request:
$('#form').submit(function(){
$.post($(this).attr('action'), { update: $(this).find('#text).val() }, function(){
// success
});
});
Checkout the jQuery API/Docs for more info on this. There are also dozens of tutorials lurking around the net on how to do this.
Well, by returning false from the event handler function, you trigger two things:
prevent the default action (.preventDefault())
stop the event propagation (.stopPropagation())
This prevents that the submit ever happens.
You need to transfer the data on your own within the submit event handler. For instance, create an ajax request which serializes the form data and sends it to your server.
Could look like:
$('#form').live('submit',function(){
$('#form').fadeOut('slow');
$('#div').append("<h2>submittes</h2>");
$.post('execute.php', $(this).serialize(), function(data) {
// do something after success
});
return false;
});