I created a minimum working example of what I'm talking about, and it's
attached. It actually compiles and runs!
Although it's 135 lines, I had to get a web server and DBO up and going.
Really, it's *remarkable* that it's so short and in C++! Anyway, when I
click the pushbutton created on line 124, it looks like the session.find on
line 112 is causing the page to refresh. I'm not sure why a Wt::Dbo call
would cause the page to reload? Or would cause the browser to issue a new
GET.
Any ideas? Thanks, all!
In Christ,
Aaron Laws
On Wed, Aug 12, 2015 at 4:26 PM, Aaron Laws <[email protected]> wrote:
> In this code: http://pastie.org/10347062, when I uncomment line 36 and
> click the new ingredient button, the page refreshes, and I'm not sure why.
> When I comment line 36, no refresh. Of course, the ingredient isn't added
> to the dbo collection, but the GUI element shows up on the page, etc.
>
> Is there anything about getting a non-const pointer to an object, and
> adding something to a Wt::Dbo::collection on that object that would cause a
> page to refresh? It has me a bit frazzled. Let me know if you have any
> other questions.
>
> In Christ,
> Aaron Laws
>
#include <Wt/WContainerWidget>
#include <Wt/WPushButton>
#include <Wt/WText>
#include <Wt/WBreak>
#include <Wt/WApplication>
#include <Wt/Dbo/Dbo>
#include <Wt/Dbo/backend/Sqlite3>
#include <Wt/WEnvironment>
#include <string>
#include <Wt/WEnvironment>
#include <Wt/WApplication>
#include <Wt/WSignal>
#include <string>
#include <Wt/Dbo/Dbo>
struct puppy;
struct trick : public Wt::Dbo::Dbo<trick>
{
std::string name;
Wt::Dbo::ptr<puppy> mypuppy;
template <class A> void persist(A&a)
{
Wt::Dbo::field(a, name, "name");
Wt::Dbo::belongsTo(a, mypuppy, "puppyid");
}
};
struct puppy : public Wt::Dbo::Dbo<puppy>
{
std::string name;
Wt::Dbo::Query<Wt::Dbo::ptr<trick>, Wt::Dbo::DynamicBinding> get_tricks()
{
return puppies.find();
}
template<class A> void persist(A&a)
{
Wt::Dbo::field(a, name, "name");
Wt::Dbo::hasMany(a, puppies, Wt::Dbo::ManyToOne, "puppyid");
}
private:
Wt::Dbo::collection<Wt::Dbo::ptr<trick> > puppies;
};
struct web_container : public Wt::WContainerWidget
{
web_container (Wt::Dbo::Session &, Wt::WContainerWidget * parent = nullptr);
Wt::Signal<> & done() { return done_; }
void print_out_puppies (void);
private:
Wt::Dbo::Session & session;
Wt::Signal<> done_;
};
struct myweb_app_test : public Wt::WApplication
{
myweb_app_test(Wt::WEnvironment const &, Wt::Dbo::Session & session);
};
Wt::Dbo::Session session;
Wt::WApplication* create_application(Wt::WEnvironment const & env)
{
return new myweb_app_test(env, session);
}
void populate_db(void)
{
{
Wt::Dbo::Transaction thetransaction{session};
puppy * p{new puppy};
p->name = "spaghetti puppy";
auto puppyptr = session.add(p);
trick * t{new trick};
t->name = "wrap up";
auto trickptr = session.add(t);
t = new trick;
t->name = "lay down";
trickptr = session.add(t);
}
}
int main(int argc, char** argv)
{
Wt::Dbo::backend::Sqlite3 db("puppies.db");
db.setProperty("show-queries", "true");
session.setConnection(db);
session.mapClass<puppy>("puppy");
session.mapClass<trick>("trick");
try
{
session.createTables();
populate_db();
std::cerr << "created new database\n";
}
catch (Wt::Dbo::Exception & e)
{
std::cerr << "New database not created because of ";
std::cerr << e.what() << ". Assuming db already exists.\n";
}
return Wt::WRun(argc, argv, &create_application);
}
void web_container::print_out_puppies (void)
{
Wt::Dbo::collection<Wt::Dbo::ptr<puppy> > puppies = session.find<puppy>();
/*for (auto & p : puppies)
{
new Wt::WText{p->name, this};
new Wt::WBreak{this};
}*/
}
web_container::web_container(Wt::Dbo::Session & session, Wt::WContainerWidget * parent)
: session(session), Wt::WContainerWidget{parent}
{
std::cerr << "\n\n\nCreating new web-container\n\n";
auto btn = new Wt::WPushButton {"text", this};
btn->clicked().connect(this, &web_container::print_out_puppies);
}
myweb_app_test::myweb_app_test(Wt::WEnvironment const & env, Wt::Dbo::Session & session)
: Wt::WApplication(env)
{
new web_container{session, root()};
}
------------------------------------------------------------------------------
_______________________________________________
witty-interest mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/witty-interest