Displaying a stream of incoming data - cannot access property of undefined - javascript

EDIT
This is the log for my request
$("#search_button").click(function(){
var search = document.getElementById('search_text').value;
$.post('http://127.0.0.1:5000/search', {search_text: search, num_results: 2}, function(data, textStatus, xhr) {
console.log(data);
});
});
Background
I'm fetching some data from my server and trying to display it on my page using the Onsen UI framework's Infinite List but I'm getting a cannot access property 'text' of undefined error. I do see the data using console.log(data) so I hope there's not a problem with the request. I would really appreciate if someone could explain me what am I doing wrong here? Thanks
This Works
I tried a basic example before fetching the data
ons.ready(function() {
var data = [{"text":"Title 1"}, {"text":"Title 2"}]
var infiniteList = document.getElementById('infinite-list');
infiniteList.delegate = {
createItemContent: function(i) {
return ons.createElement(`<ons-list-item>
<p style="color:DodgerBlue;">`+data[i].text+`</p>
<img style="width:100%;" src="http://images.all-free-download.com/images/graphiclarge/beautiful_scenery_04_hd_pictures_166258.jpg"/>
</ons-list-item>`);
},
countItems: function() {
return data.length;
}
};
infiniteList.refresh();
});
This doesnt work
ons.ready(function(){
$("#test_button").click(function(){
$.post('http://127.0.0.1:5000/search', {search_text: 'car', num_results: 2}, function(data, textStatus, xhr) {
/*after success */
var infiniteList = document.getElementById('infinite-list');
infiniteList.delegate = {
createItemContent: function(i) {
return ons.createElement(`<ons-list-item>
<p style="color:DodgerBlue;">`+data[i]+`</p>
<img style="width:100%;" src="http://images.all-free-download.com/images/graphiclarge/beautiful_scenery_04_hd_pictures_166258.jpg"/>
</ons-list-item>`);
},
countItems: function() {
return 2;
}
};
infiniteList.refresh();
});
});
});

'Data' is limited to the function in which it is visible; it needs to be recognized where you are using it as well.
Use another variable which is declared outside both functions.
var dataUse = [];//----------declare here so it is visible in every scope
ons.ready(function(){
$("#test_button").click(function(){
$.post('http://127.0.0.1:5000/search', {search_text: 'car', num_results: 2}, function(data, textStatus, xhr) {
/*after success */
dataUse = data;
var infiniteList = document.getElementById('infinite-list');
infiniteList.delegate = {
createItemContent: function(i) {
return ons.createElement(`<ons-list-item>
<p style="color:DodgerBlue;">`+dataUse[i]+`</p>
<img style="width:100%;" src="http://images.all-free-download.com/images/graphiclarge/beautiful_scenery_04_hd_pictures_166258.jpg"/>
</ons-list-item>`);
},
countItems: function() {
return 2;
}
};
infiniteList.refresh();
});
});
});

Related

Neither $scope.$apply and $scope.$digest updates the objects

I'm sing the following ng-click function to create groups:
$scope.createGroup = function() {
AV.Cloud.run('createUserGroup', {
groupName: $scope.initName
}, {
success: function(result) {
var data = JSON.parse(result);
var json = {
id: data.objectId,
name: data.name,
users: 0,
createdAt: data.createdAt
}
$scope.groups.push(json);
$scope.initName = "";
$scope.$digest();
},
error: function(error) {
alert(error.message);
}
});
But then when I click the link to go to the group (which is in the same page) the group doesn't appear. I have to refresh the browser in order to make it appear. I tried $scope.$digest() and $scope.$apply() but they didn't solve the problem, only location.reload(); does.
Does AngularJS have another function to make an update or reload that might solve this problem?
EDIT:
This is how I'm displaying the group:
<div id="group-new" class="col-md-6">
<h2>{{group.name}}</h2>
</div>
Have you tried wrapping it up in $scope.$apply, it should both apply the changes to your $scope and digest afterwards. Example:
$scope.createGroup = function() {
AV.Cloud.run('createUserGroup', {
groupName: $scope.initName
}, {
success: function(result) {
var data = JSON.parse(result);
var json = {
id: data.objectId,
name: data.name,
users: 0,
createdAt: data.createdAt
}
$scope.$apply(function() {
$scope.groups.push(json);
$scope.initName = "";
});
},
error: function(error) {
alert(error.message);
}
});
How did you use $scope.$apply?
Did you try?
success: function(result) {
// ...
$scope.$apply(function () {
$scope.groups.push(json);
$scope.initName = "";
});
}
Maybe there is already a $digest cycle running?
Try $timeout (you have to inject it into to your controller):
$timeout
success: function(result){
// ...
$timeout(function(){
$scope.groups.push(json);
scope.initName = "";
});
}
Basically it delays the execution (in the above case: 0ms) of the inner function and triggers a new digest cycle with $apply. It does not create "$digest already in progress" errors because it starts a new digest after the running one is finished.

Jquery Context Menu ajax fetch menu items

I have a jquery context menu on my landing page where I have hardcode menu items. Now I want to get the menu items from server. Basically the idea is to show file names in a specified directory in the context menu list and open that file when user clicks it...
This is so far I have reached..
***UPDATE***
C# code
[HttpPost]
public JsonResult GetHelpFiles()
{
List<Manuals> manuals = null;
var filesPath = Server.MapPath(#"\HelpManuals");
var standardPath = new DirectoryInfo(filesPath);
if (standardPath.GetFiles().Any())
{
manuals = standardPath.GetFiles().Select(x => new Manuals
{
Name = GetFileNamewithoutExtension(x.Name),
Path = x.Name
}).ToList();
}
return Json(manuals, JsonRequestBehavior.AllowGet);
}
private string GetFileNamewithoutExtension(string filename)
{
var extension = Path.GetExtension(filename);
return filename.Substring(0, filename.Length - extension.Length);
}
JavaScript Code
$.post("/Home/GetHelpFiles", function (data) {
$.contextMenu({
selector: '#helpIcon',
trigger: 'hover',
delay: 300,
build: function($trigger, e) {
var options = {
callback: function(key) {
window.open("/HelpManuals/" + key);
},
items: {}
};
$.each(data, function (item, index) {
console.log("display name:" + index.Name);
console.log("File Path:" + index.Path);
options.items[item.Value] = {
name: index.Name,
key: index.Path
}
});
}
});
});
Thanks to Matt. Now, the build function gets fire on hover.. but im getting illegal invocation... and when iterating through json result, index.Name and this.Name gives correct result. But item.Name doesn't give anything..
to add items to the context menu dynamically you need to make a couple changes
$.contextMenu({
selector: '#helpIcon',
trigger: 'hover',
delay: 300,
build: function($trigger, e){
var options = {
callback: function (key) {
var manual;
if (key == "adminComp") {
manual = "AdminCompanion.pdf";
} else {
manual = "TeacherCompanion.pdf";
}
window.open("/HelpManuals/" + manual);
},
items: {}
}
//how to populate from model
#foreach(var temp in Model.FileList){
<text>
options.items[temp.Value] = {
name: temp.Name,
icon: 'open'
}
</text>
}
//should be able to do an ajax call here but I believe this will be called
//every time the context is triggered which may cause performance issues
$.ajax({
url: '#Url.Action("Action", "Controller")',
type: 'get',
cache: false,
async: true,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (_result) {
if (_result.Success) {
$.each(_result, function(item, index){
options.items[item.Value] = {
name: item.Name,
icon: 'open'
}
});
}
});
return options;
}
});
so you use build and inside of that define options and put your callback in there. The items defined in there is empty and is populated in the build dynamically. We build our list off of what is passed through the model but I believe you can put the ajax call in the build like I have shown above. Hopefully this will get you on the right track at least.
I solved this problem the following way.
On a user-triggered right-click I return false in the build-function. This will prevent the context-menu from opening. Instead of opeing the context-menu I start an ajax-call to the server to get the contextMenu-entries.
When the ajax-call finishes successfully I create the items and save the items on the $trigger in a data-property.
After saving the menuItems in the data-property I open the context-menu manually.
When the build-function is executed again, I get the items from the data-property.
$.contextMenu({
build: function ($trigger, e)
{
// check if the menu-items have been saved in the previous call
if ($trigger.data("contextMenuItems") != null)
{
// get options from $trigger
var options = $trigger.data("contextMenuItems");
// clear $trigger.data("contextMenuItems"),
// so that menuitems are gotten next time user does a rightclick
// from the server again.
$trigger.data("contextMenuItems", null);
return options;
}
else
{
var options = {
callback: function (key)
{
alert(key);
},
items: {}
};
$.ajax({
url: "GetMenuItemsFromServer",
success: function (response, status, xhr)
{
// for each menu-item returned from the server
for (var i = 0; i < response.length; i++)
{
var ri = response[i];
// save the menu-item from the server in the options.items object
options.items[ri.id] = ri;
}
// save the options on the table-row;
$trigger.data("contextMenuItems", options);
// open the context-menu (reopen)
$trigger.contextMenu();
},
error: function (response, status, xhr)
{
if (xhr instanceof Error)
{
alert(xhr);
}
else
{
alert($($.parseHTML(response.responseText)).find("h2").text());
}
}
});
// This return false here is important
return false;
}
});
I have finally found a better solution after reading jquery context menu documentation, thoroughly..
C# CODE
public JsonResult GetHelpFiles()
{
List<Manuals> manuals = null;
var filesPath = Server.MapPath(#"\HelpManuals");
var standardPath = new DirectoryInfo(filesPath);
if (standardPath.GetFiles().Any())
{
manuals = standardPath.GetFiles().Select(x => new Manuals
{
Name = GetFileNamewithoutExtension(x.Name),
Path = x.Name
}).ToList();
}
return Json(manuals, JsonRequestBehavior.AllowGet);
}
HTML 5
<div id="dynamicMenu">
<menu id="html5menu" type="context" style="display: none"></menu>
</div>
JavaScript Code
$.post("/Home/GetHelpFiles", function (data) {
$.each(data, function (index, item) {
var e = '<command label="' + item.Name + '" id ="' + item.Path + '"></command>';
$("#html5menu").append(e);
});
$.contextMenu({
selector: '#helpIcon',
trigger: 'hover',
delay: 300,
items: $.contextMenu.fromMenu($('#html5menu'))
});
});
$("#dynamicMenu").on("click", "menu command", function () {
var link = $(this).attr('id');
window.open("/HelpManuals/" + link);
});
Here's my solution using deferred, important to know that this feature is supported for sub-menus only
$(function () {
$.contextMenu({
selector: '.SomeClass',
build: function ($trigger, e) {
var options = {
callback: function (key, options) {
// some call back
},
items: JSON.parse($trigger.attr('data-storage')) //this is initial static menu from HTML attribute you can use any static menu here
};
options.items['Reservations'] = {
name: $trigger.attr('data-reservations'),
icon: "checkmark",
items: loadItems($trigger) // this is AJAX loaded submenu
};
return options;
}
});
});
// Now this function loads submenu items in my case server responds with 'Reservations' object
var loadItems = function ($trigger) {
var dfd = jQuery.Deferred();
$.ajax({
type: "post",
url: "/ajax.php",
cache: false,
data: {
// request parameters are not importaint here use whatever you need to get data from your server
},
success: function (data) {
dfd.resolve(data.Reservations);
}
});
return dfd.promise();
};

GUI component for display async request states

Which interface or component do you suggest to display the state of parallel async calls? (The language is not so important for me, just the pattern, I can rewrite the same class / interface in javascript...)
I load model data from REST service, and I want to display pending label before the real content, and error messages if something went wrong... I think this is a common problem, and there must be an already written component, or best practices, or a pattern for this. Do you know something like that?
Here is a spaghetti code - Backbone.syncParallel is not an existing function yet - which has 2 main states: updateForm, updated. Before every main state the page displays the "Please wait!" label, and by error the page displays an error message. I think this kind of code is highly reusable, so I think I can create a container which automatically displays the current state, but I cannot decide what kind of interface this component should have...
var content = new Backbone.View({
appendTo: "body"
});
content.render();
var role = new Role({id: id});
var userSet = new UserSet();
Backbone.syncParallel({
models: [role, userSet],
run: function (){
role.fetch();
userSet.fetch();
},
listeners: {
request: function (){
content.$el.html("Please wait!");
},
error: function (){
content.$el.html("Sorry, we could not reach the data on the server!");
},
sync: function (){
var form = new RoleUpdateForm({
model: role,
userSet: userSet
});
form.on("submit", function (){
content.$el.html("Please wait!");
role.save({
error: function (){
content.$el.html("Sorry, we could not save your modifications, please try again!");
content.$el.append(new Backbone.UI.Button({
content: "Back to the form.",
onClick: function (){
content.$el.html(form.$el);
}
}));
},
success: function (){
content.$el.html("You data is saved successfully! Please wait until we redirect you to the page of the saved role!");
setTimeout(function (){
controller.read(role.id);
}, 2000);
}
});
}, this);
form.render();
content.$el.html(form.$el);
}
}
});
I created a custom View to solve this problem. (It is in beta version now.)
Usage: (Form is a theoretical form generator)
var content = new SyncLabelDecorator({
appendTo: "body",
});
content.load(function (){
this.$el.append("normal html without asnyc calls");
});
var User = Backbone.Model.extend({
urlRoot: "/users"
});
var UserSet = Backbone.Collection.extend({
url: "/users",
model: User
});
var Role = Backbone.RelationalModel.extend({
relations: [{
type: Backbone.HasMany,
key: 'members',
relatedModel: User
}]
});
var administrator = new Role({id :1});
var users = new UserSet();
content.load({
fetch: [role, users],
sync: function (){
var form = new Form({
title: "Update role",
model: role,
fields: {
id: {
type: "HiddenInput"
},
name: {
type: "TextInput"
},
members: {
type: "TwoListSelection",
alternatives: users
}
},
submit: function (){
content.load({
tasks: {
save: role
},
sync: function (){
this.$el.html("Role is successfully saved.");
}
});
}
});
this.$el.append(form.render().$el);
}
});
Code:
var SyncLabelDecorator = Backbone.View.extend({
options: {
pendingMessage: "Sending request. Please wait ...",
errorMessage: "An unexpected error occured, we could not process your request!",
load: null
},
supported: ["fetch", "save", "destroy"],
render: function () {
if (this.options.load)
this.load();
},
load: function (load) {
if (load)
this.options.load = load;
this._reset();
if (_.isFunction(this.options.load)) {
this.$el.html("");
this.options.load.call(this);
return;
}
_(this.options.load.tasks).each(function (models, method) {
if (_.isArray(models))
_(models).each(function (model) {
this._addTask(model, method);
}, this);
else
this._addTask(models, method);
}, this);
this._onRun();
_(this.tasks).each(function (task) {
var model = task.model;
var method = task.method;
var options = {
beforeSend: function (xhr, options) {
this._onRequest(task, xhr);
}.bind(this),
error: function (xhr, statusText, error) {
this._onError(task, xhr);
}.bind(this),
success: function (data, statusText, xhr) {
this._onSync(task, xhr);
}.bind(this)
};
if (model instanceof Backbone.Model) {
if (method == "save")
model[method](null, options);
else
model[method](options);
}
else {
if (method in model)
model[method](options);
else
model.sync(method == "fetch" ? "read" : (method == "save" ? "update" : "delete"), model, options);
}
}, this);
},
_addTask: function (model, method) {
if (!_(this.supported).contains(method))
throw new Error("Method " + method + " is not supported!");
this.tasks.push({
method: method,
model: model
});
},
_onRun: function () {
this.$el.html(this.options.pendingMessage);
if (this.options.load.request)
this.options.load.request.call(this);
},
_onRequest: function (task, xhr) {
task.abort = function () {
xhr.abort();
};
},
_onError: function (task, xhr) {
this._abort();
this.$el.html(this.options.errorMessage);
if (this.options.load.error)
this.options.load.error.call(this);
},
_onSync: function (task, xhr) {
++this.complete;
if (this.complete == this.tasks.length)
this._onEnd();
},
_onEnd: function () {
this.$el.html("");
if (this.options.load.sync)
this.options.load.sync.call(this);
},
_reset: function () {
this._abort();
this.tasks = [];
this.complete = 0;
},
_abort: function () {
_(this.tasks).each(function (task) {
if (task.abort)
task.abort();
});
}
});

Backbonejs when to initialize collections

I'm building small one page application with rails 3.1 mongodb and backbonejs.
I have two resources available through json api. I created two models and collections in backbone which look like this
https://gist.github.com/1522131
also I have two seprate routers
projects router - https://gist.github.com/1522134
notes router - https://gist.github.com/1522137
I generated them with backbonejs-rails gem from github so code inside is just template. I initialize my basic router inside index.haml file
#projects
:javascript
$(function() {
window.router = new JsonApi.Routers.ProjectsRouter({projects: #{#projects.to_json.html_safe}});
new JsonApi.Routers.NotesRouter();
Backbone.history.start();
});
I don't want fetch notes when application is starting, because there is big chance that user will never look inside notes. So there isn't good reason to fetch it on start. Inside NotesRouter in all action I rely on #notes variable but without .fetch() method this variable is empty. Also I should can reproduce notes view from url like
/1/notes/5
project_id = 1
note_id = 5
What is best practices in backbonejs to solve this kind of problem ?
Why don't you lazy load the notes when it's requested? Here's an example:
var State = Backbone.Model.extend({
defaults: {
ready: false,
error: null
}
});
var Note = Backbone.Model.extend({
initialize: function () {
this.state = new State();
}
});
var Notes = Backbone.Collection.extend({
model: Note,
initialize: function () {
this.state = new State();
}
});
var NoteCache = Backbone.Model.extend({
initialize: function () {
this._loading = false;
this._loaded = false;
this._list = new Notes();
},
_createDeferred: function (id) {
var note = new Note({ id: id });
this._list.add(note);
this._load();
return note;
},
getNote: function (id) {
return this._list.get(id) || this._createDeferred(id);
},
getNotes: function () {
if (!this._loaded)
this._load();
return this._list;
},
_load: function () {
var that = this;
if (!this._loading) {
this._list.state.set({ ready: false, error: null });
this._loading = true;
$.ajax({
url: '/api/notes',
dataType: 'json',
cache: false,
type: 'GET',
success: function (response, textStatus, jqXHR) {
_.each(response.notes, function (note) {
var n = that._list.get(note.id);
if (n) {
n.set(note);
} else {
that._list.add(note, { silent: true });
n = that._list.get(note.id);
}
n.state.set({ ready: true, error: null });
});
that._list.state.set({ ready: true, error: null });
that._list.trigger('reset', that._list);
that._loaded = true;
},
error: function (jqXHR, textStatus, errorThrown) {
that._list.state.set({ error: 'Error retrieving notes.' });
that._list.each(function (note) {
note.state.set({ error: 'Error retrieving note.' });
});
},
complete: function (jqXHR, textStatus) {
that._loading = false;
}
});
}
}
});
In this example, I'm defining a NoteCache object that manages the lazy loading. I also add a "state" property to the Note model and Notes collection.
You'll probably want to initialize NoteCache somewhere (probably inside your route) and whenever you want a note or notes, just do this:
var note = noteCache.getNote(5);
var notes = noteCache.getNotes();
Now inside your view, you'll want to listen for state changes in case the note/notes is not loaded yet:
var NoteView = Backbone.View.extend({
initialize: function(){
this.note.state.bind('change', this.render, this);
},
render: function(){
if (this.note.state.get('error') {
// todo: show error message
return this;
}
if (!this.note.state.get('ready') {
// todo: show loader animation
return this;
}
// todo: render view
return this;
}
});
I haven't tested this, so there may be some bugs, but I hope you get the idea.

Javascript scoping issue

I am trying to build a generic ajax loader, while ajax is running a lightbox with animated "Loading" gif will be displayed.
I have some issues with scoping.
The code is:
var t=setTimeout( "s.d.dialog( 'destroy' )" ,(s.o.msgTime*1000));
The error is: "Uncaught ReferenceError: s is not defined"
;(function ($) {
$.loader = function (data, options) {
return $.loader.impl.init(data, options);
};
$.loader.close = function (data) {
$.loader.impl.close(data);
};
$.loader.create = function () {
$.loader.impl.create();
};
$.loader.defaults = {
appendTo: 'body',
autoCreate: true,
msgTime: 5,
};
$.loader.impl = {
d: {},
init: function(data, options){
var s = this;
s.o = $.extend({}, $.loader.defaults, options);
if ((typeof data === 'object')&&!(data instanceof jQuery)&&data.url) {
data.success = function(data, textStatus, jqXHR){ $.loader.close(); }
data.error = function(jqXHR, textStatus, errorThrown){ $.loader.close('Error accessing server'); }
$.ajax(data);
}else if(s.o.autoCreate){
s.create();
}
return s;
},
create: function() {
var s = this;
s.d = $('<div id="dialog" style="display:hidden"><span style="width: 100%" id="loading_diag"><center><img src="http://www.mydomain.com/images/ajax-loader.gif" /></center></span></div>').appendTo(s.o.appendTo);
s.d.dialog({ title: 'Loading ...', dialogClass: 'noTitleStuff', modal: true, draggable: false, resizable: false });
},
close: function(data)
{
var s = this;
//alert(typeof s.d);
if ((typeof data === 'string')&&data) {
$("#loading_diag").hide();
$("#dialog").html(data);
var t=setTimeout( "s.d.dialog( 'destroy' )" ,(s.o.msgTime*1000));
}else{
s.d.dialog( "destroy" );
}
s.d= {};
},
};
})(jQuery);
If anybody knows how to solve it please share.
The first and second solution did something but havent fixed it completely,
now i am getting a different error: "Uncaught TypeError: Object # has no method 'dialog' $.loader.impl.close.s.d"
This will make it work:
var t = setTimeout(function() { s.d.dialog('destroy'); }, s.o.msgTime * 1000);
When you pass a string into setTimout, then that string (code) executes in global code - and since s is a local variable, it is indeed not defined in global code.
When you pass a string to setTimeout, the code in the string is executed in the context of the window object. Since window.s doesn't exist, you get the error. You can pass a closure to setTimeout to keep your s variable in scope like this:
var t = setTimeout(function() {s.d.dialog('destroy'); }, s.o.msgTime * 1000);

Categories

Resources