Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 4 years ago.
Improve this question
I'm kind of sick of using JSON/Rest services lately and manually typing up methods on the server that do basic CRUD operations on a database.
What I'd like to do, is in javascript (ajax based apps) do something of the form
var allStudents = students.getAllStudents(); // returns all items of the students table
var student = new student();
student.name = "Joe";
student.address = "123 Sesame st";
students.add(student); // commits it to the students table
var student = students.getStudentById(57);
Now as any ORM all these methods would be automated/written for me.
Also note that I'm not saying Javascript should talk directly to a database. It would
still do Restful calls (behind the scenes to a server). But I just want
these crud operations to be automated and transparent to me so that I don't need to
manually write out these on the server.
You guys know any frameworks that would help achieve this?
My primary backend is Java/Spring3MVC. But I'd also like to hear ideas that use
Node.js possibly.
I'm undecided on whether or not this is a time-saver compared to simply writing the RESTful ajax requests out, but Dojo's JsonRest store is one solution I've seen which works similarly to what you're describing. Personally I find it more readable to write the ajax request explicitly, but if you don't mind adhering to Dojo's philosophy on how your requests should be structured you might like this. Anyhow, here's some code from that documentation page:
require(["dojo/store/JsonRest"], function(JsonRestStore){
var store = new JsonRestStore({target: "/Table/" });
store.get(3).then(function(object){
// use the object with the identity of 3
});
store.query("foo=bar").then(function(results){
// use the query results returned from the server
});
store.put({ foo: "bar" }, { id: 3 }); // store the object with the given identity
store.remove(3); // delete the object
});
If you can use something like Backbone.js or Can.js (recommended) to do your interfaces and seamlessly communicate with your database through RESTfull services in a way you will be impressed if you haven't seen it before.
http://backbonejs.org/
http://canjs.us/
Both use a MVC structure that is incredibly easy to setup.
Take a look at the demos and examples.
Looking for the same thing, I have stumbled upon sproutcore records. Look like a javascript orm solution.
Related
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
I wanna use php variable in frontend framework like Vue js.
What is the best way of integration frontend and backend framework?
This is my idea, but i think there are better way to do this.
<script id = "data" >
let $user = <?= json_encode($user) ?>
</script >
Some content...
<script >
new Vue({
data: {
user: $user
},
mounted() {
$("#data"). remove ()
}
})
While 'simplicity' is wonderful, 'functionality' is also pretty critical...
Sometimes you can get by with your type of coding (use it for some things that come into the PHP file that are needed to load the page, for example), and what you have may work for this particular situation (and, no, there isn't any way I can see to make it "better"...), though most pages will need more data that is 'fluid', and you will quickly run out of projects where you can write only 'simple' code.
Learn to use ajax (it is pretty simple once you get the hang of it) and copy/paste from your own 'library' (save snippets in a place you remember - you will find MANY things you want to keep... - I keep a 'functions.php' file and over the years it has grown pretty large with great bits-n-pieces.)
Since you are using jQuery already, here's one way to do ajax... (there are others, again, study and find the way you like...)
var url = "https://theURLtoMyAjax.phpPage";
var elements = "theStuff=thatIwantToSend&someMore=somethingElse"; // this is like writing everything in the address bar - again, there are other ways...)
$.post(url, elements, function (data) {
// do all kinds of wonderful things in here!
// the 'data' is what is returned from your call, so you can use it to update data on the page, etc.
});
So, as you can see, only a couple lines of code to add Ajax and tons of things you can do once you do it, so learn it - and use it!
Happy coding!
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I'm starting out php.
I'm wondering how many ajax files should I have. Should I have a seperate one for each operation I want to do? each query insert etc,
Or do I like send something in the data, or maybe request that ID's the request so that the server knows what to do?
Is there a good example for that?
I don't know if it matters but I'm using jQuery.
To answer your question, I personally like having as many files as possible (with fewer lines of code), but keeping related functions groupped in an object inside the same file.
For example, you could have one file called userAjax.js which contains the userAjax object:
var userAjax = {
getUserLevel : function (userId) {
$.get // blah, blah, or any ajax request
},
setUsername : function (userId, username) {
$.get // blah, blah, or any ajax request
}
};
In your app you could then use (after including the userAjax.js):
userAjax.setUsername(37, "John");
I like using this method because it keeps code structured, you do not have too much code for too little functionality. I use it in small to medium sized projects and works great :) (both for production & maintanance).
For the server-side, you could either do the same thing, or simply have a file for each command. I also like file-per-command method because if you structure your files in folders it's very easy to maintain the code (you can go directly to the function you want by navigating through the file tree). But again, for larger projects I think you should use a more OOP-approach, like having a class with many functions in a single file.
To sum it up, it all depends, mostly based on the size of the project.
Well, you can create functions for all operations what you want to do, and handle this functions with one file. Or you can create as many as want files for handling requests. If you are using some framework built on MVC architecture, you will probably use only one file (Controller) or more functions in more controllers, it is really variable, depending on usage.
There is lot of tutorials how to use PHP with AJAX. You just need only search for them.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 4 months ago.
This post was edited and submitted for review 4 months ago and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
I'm starting a new Titanium app and want to use best practices and avoid having memory leaks from the get go. I'm new to CommonJS as well as the Titanium platform in general.
Unfortunately it seems that all the sample applications for titanium surround on Ti.include("/lib/module") instead of the newer recommended best practice of require("/lib/module").
What I am worried about is the memory consumption of using CommonJS could require. From the CommonJS Modules in Titanium documentation it states that modules will be cached, wouldn't this mean that if I ever access a module that all those functions suddenly stay around in memory even if they go out of scope?
I've started a new app with the following structure
/ctrl # Model/UI controllers
/lib # libraries (common + 3rd party)
/ui # UI forms
/model # DAL objects for data store
From here my main app has a single dashboard style view which is loosely structured as follows:
(function() {
var getMenuItem = require("/ui/main").getMenuItem;
var win = Titanium.UI.createWindow({
title:'Main',
backgroundColor:'#fff'
});
var nav = Ti.UI.iPhone.createNavigationGroup({
window:win
});
var sect;
var data = [];
sect = Ti.UI.createTableViewSection();
data.push(sect);
sect.add(getMenuItem("Customers",
require("/ctrl/account").createCustMainWindow));
sect.add(getMenuItem("Schedules",
require("/ctrl/schedule").createScheduleMainWindow));
sect.add(getMenuItem("Settings"));
var menu = Titanium.UI.createTableView({
style: Ti.UI.iPhone.TableViewStyle.GROUPED,
data:data
});
win.add(menu);
menu.addEventListener('click',function(e) {
if (e.rowData.createWindow) {
var win = e.rowData.createWindow(nav);
nav.open(win);
}
});
var navWindow = Titanium.UI.createWindow();
navWindow.add(nav);
navWindow.open();
})();
Any guidance on a correct project structure is greatly appreciated.
This is the community app under development that purely uses module pattern, also look through the developer blog to find Forging series which have samples developed mostly using module pattern
I use require() and I have a long start of require() at the beginning of the project - wich might be one of the biggest Titanium project.
And I can be confident in saying that there is no visible cost in putting tousands of lines of code in these require statements.
However, you must constently check that your code has no memory leaks.
AFAIK the memory used by CommonJS module is released whenever the window is properly closed. Unless you assign the module to a global object, in that case it wouldn't be a leak.
Personally, I create a reusable factory. Actually, it depends on how complex the app is, if it's just a couple windows within NavGroup, I just write it straight up.
And don't forget to profile your app with Xcode instruments to make sure your app isn't leaking and if it does, it'll help finding the leak. Watch this video for demo (about 1/3 way in).
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
We are always trying to improve on our ability to apply our skills to solve a problem. Software engineering principles have significantly helped my ability to write higher quality code. This includes testing, modularization, using OO where appropriate, etc.
Here's an example of how I achieved some modularization in my JS. Maybe it is a bad way to achieve this, but it serves as an example of what I mean and contains a few questions of its own.
framework.js
Framework = {
CurrentState : {
IsConfigurationLoaded : false,
IsCurrentConfigurationValid : false,
Configuration : undefined //undefined .. good? bad? undefined?
},
Event : {
//event lib
},
//you get the idea
}
Question:
In what ways do you apply software engineering principles to improve the readability, maintainability, and other quality attributes of your JS?
Other Related (more specific) Questions to help in answering:
I had once written a simple JS unit testing framework, which had simple asserts and a test helper method taking a lambda. What are your thoughts on unit testing javascript?
How important is defining the boundary between your code and framework?
JS is mostly used in a browser or in a website. Does this reduce/nullify certain concerns?
Do you suggest usage of Classes and OO principles?
Usage of undefined and/or null? Should it be forbidden?
Usage of try/catch? Suggested?
When do you go from JSON to classes? Do you use Util methods that operate on the data?
Usage of prototype? Suggested? What is a good case where you wouldn't use it?
in large project i tend to differ between model-, control- and view-files ([mvc-pattern][1]).
the model-file contains everything concerning data especially my class (OOP). an example for a model-file could be:
function myClass(){
//private variable
var variable=5;
//public variable
this.newVariable = 10;
function myFunction() {
//do some stuff
alert("my function");
}
//public stuff
return {
myPublicFunction: myFunction
}
}
the view-file contains everything concerning to the layout and view and the control-file is filled with the stuff concerning to data-handling. control-file uses the class declared in the model-file and works with it. so the view only needs to include the control-file and call the functions it needs for the layout.
but in general it's quite different to generalize. i like the oo-pattern and trie to use is, if it makes sense. but i have only experience with iPhone-development, so i am not able to say something concerning web dev.
[1]: http://en.wikipedia.org/wiki/Model%E2%80%93View%E2%80%93Controller
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 5 years ago.
Improve this question
I very like MySQLs mysql cli tool and I don't like phpMyAdmin.
[IMHO]It's a nice thing for a Windows user, but its not so good when you've used to console.[/IMHO].
What I want is to build a web page containing element with console-like input (for example something like this) which should get input from user, send it to PHP script on back-end and show back-end response.
Back-end script is done (it was the easiest part), but I can't find any library for JavaScript implementing console-like input.
I've tried to examine and modify for my needs example I've provided, but it's too bloated (because doesn't use any libraries) and implements specific thing. Also I would like this element to provide some auto-completion for input.
Any ideas on such JS library?
I think you are looking for this: jQueryTerminal
there is shellinabox - javascript terminal.
EDIT:
There is also library xterm.js that's real terminal emulator.
EDIT 2:
My jQuery Terminal library is useful when you need custom behavior and you can write your code in JS or as backend code, but backend need to be simple input -> output, if you want to run for instance interactive backend commands, like vi or emacs, you need proper tty, for this use xterm.js (or implement that in JavaScript) for any other usage jQuery Terminal is better. It have lot of features and you don't need to run process on the server (listen on a port) which usually is forbidden on shared hostings or GitHub pages.
instead of using console.log() use document.write()
It will write text on the webpage just like console.log would in the console
I've made a console library called Simple Console (I'll probably rename it because simple-console is taken on npm)
It handles command history and such for you, and you can use it to implement any kind of console.
var handleCommand = (command)=> {
var req = new XMLHttpRequest();
req.addEventListener("load", ()=> {
con.log(req.responseText);
// TODO: use con.error for errors and con.warn for warnings
// TODO: maybe log a table element to display rows of data
});
// TODO: actually pass the command to the server
req.open("GET", "mysql.php");
req.send();
};
var con = new SimpleConsole({
handleCommand,
placeholder: "Enter MySQL queries",
storageID: "mysql-console"
});
document.body.appendChild(con.element);
Check out the documentation on GitHub for more information.
hmm firebug console ?
http://getfirebug.com/commandline