我必须根据某些条件构建查询.有没有比我在下面做的更好的方式呢?它工作正常,但我可以看到,如果有更多的条件,它会相当快地失控,因为我检查每次检查新的条件是否满足任何先前的条件.
$sql = "SELECT DISTINCT fkRespondentID FROM tblRespondentDayTime";
if (!empty($day) || !empty($time) || !empty($sportID)) {
$sql .= " WHERE";
if (!empty($day)) {
$sql .= " fldDay='$day'";
}
if (!empty($time)) {
if (!empty($day)) {
$sql .= " AND";
}
$sql .= " fldTime='$time'";
}
if (!empty($sportID)) {
if (!empty($day) || !empty($time)) {
$sql .= " AND";
}
$sql .= " fkRespondentID IN (SELECT fkRespondentID FROM tblRespondentSport WHERE fkSportID='$sportID')";
}
}
Run Code Online (Sandbox Code Playgroud) 我已经实现find(),并findAll()在我的属性模型的方法.这两种方法都对API进行异步调用. findAll()在为我的家庭路线连接插座时调用,并且工作正常. find()在连接我的物业路线的出口时,Ember.js会调用它.请注意,find()在通过操作导航到属性路径时不会调用,但是当您通过URL直接访问路径时会调用此函数.
这是我的路由器:
App.Router = Ember.Router.extend({
root: Ember.Route.extend({
showProperty: Ember.Route.transitionTo('property'),
home: Ember.Route.extend({
route: '/',
connectOutlets: function(router) {
router.get('applicationController').connectOutlet('home', App.Property.findAll());
}
}),
property: Ember.Route.extend({
route: '/property/:property_id',
connectOutlets: function(router, property) {
router.get('applicationController').connectOutlet('property', property);
}
}),
})
});
Run Code Online (Sandbox Code Playgroud)
以下是我findAll()和find()方法:
App.Property.reopenClass({
find: function(id) {
var property = {};
$.getJSON('/api/v1/property/' + id, function(data) {
property = App.Property.create(data.property);
});
return property;
},
findAll: function() {
var properties = [];
$.getJSON('/api/v1/properties', function(data) {
data.properties.forEach(function(item) …Run Code Online (Sandbox Code Playgroud) 我有一个包含两列的布局。左栏垂直滚动,右栏固定。右列包含多个部分。
在某些时候,将显示叠加层。我希望它涵盖除右栏 ( <aside class="one">...</aside>) 中第一部分之外的所有内容。
现在,当显示覆盖层时,它会覆盖所有内容,包括aside.one元素,即使它具有更高的 z-index 值。
HTML:
<!-- ... -->
<section id="sidebar">
<aside class="one">...</aside>
<aside class="two">...</aside>
</section>
<!-- ... -->
<div class="overlay"></div>
Run Code Online (Sandbox Code Playgroud)
CSS:
#sidebar {
position: fixed;
}
.one {
z-index: 3;
}
.overlay {
bottom: 0;
left: 0;
position: absolute;
right: 0;
top: 0;
z-index: 2;
}
Run Code Online (Sandbox Code Playgroud)
这是一个 jsFiddle 说明了我的问题的更完整示例:http://jsfiddle.net/ncSWz/10/
我知道我需要更改一些 HTML 结构才能使其正常工作,但我不确定从哪里开始。
提前致谢。
在我的应用程序中,我具有许多呈现不同效果的元素。我创建了一个抽象类,它们在其中进行了扩展。
abstract class Element {
protected $_value = null;
public function __construct($value) {
$this->_value = $value;
}
// ...
public abstract function render();
}
Run Code Online (Sandbox Code Playgroud)
元素的示例可能是一些包含在段落标签中的文本。
class TextElement extends Element {
public function render() {
return "<p>{$this->_value}</p>\n";
}
}
Run Code Online (Sandbox Code Playgroud)
我在创建具有多个值的元素时遇到了麻烦。例如,图像元素可能会渲染图像标签并包含多个属性。这是一个问题,因为抽象类中的构造函数仅接受一个参数。我看到了两个可能的解决方案。我可以将包含不同属性的数组传递给Element构造函数(解决方案1),也可以覆盖子类中的构造函数(解决方案2)。我的问题是,这些解决方案中的哪一个是更好的设计,或者它们是否存在更好的解决方案?我应该改用接口吗?
解决方案1
class ImageElement extends Element {
public function render() {
return "<img src=\"{$this->_value['src']}\" alt=\"{$this->_value['alt']}\" />";
}
}
$imageElement = new ImageElement(array('src' => '/image.png', 'alt' => 'image'));
Run Code Online (Sandbox Code Playgroud)
解决方案2
class ImageElement extends Element {
protected $_alt;
public function __construct($src, $alt) {
$this->_value = …Run Code Online (Sandbox Code Playgroud) 我注意到我的所有模型看起来非常相似.他们中的大多数倾向于遵循一种模式,在这种模式中,它们是包含活动记录代码的方法集合,这些代码只是彼此之间的微小变化 这是一个例子:
class Site extends CI_Model {
public function get_site_by_id($id)
{
// Active record code to get site by id
}
public function get_sites_by_user_id($user_id)
{
// ...
}
// ...
public function get_site_by_user_id_and_url_string($user_id, $url_string)
{
// ...
}
// Non active record methods and business logic
// ...
}
Run Code Online (Sandbox Code Playgroud)
这种方法对我来说很好,但我想知道是否有更优雅的解决方案.我似乎不应该每次需要以新的方式查找数据时创建一个新方法.这是常见做法还是我错过了重构这种方法的方法?
灰烬似乎无法找到findAll()和find()我有我的属性模型实现的方法.以下是我得到的错误:
TypeError: App.Property.findAll is not a function
Run Code Online (Sandbox Code Playgroud)
和
Error: assertion failed: Expected App.Property to implement `find` for use in 'root.property' `deserialize`. Please implement the `find` method or overwrite `deserialize`.
Run Code Online (Sandbox Code Playgroud)
我的路由器设置如下:
App.Router = Ember.Router.extend({
showProperty: Ember.Route.transitionTo('property'),
root: Ember.Route.extend({
home: Ember.Route.extend({
route: '/',
connectOutlets: function(router) {
router.get('applicationController').connectOutlet('home', App.Property.findAll());
}
}),
property: Ember.Route.extend({
route: '/property/:property_id',
connectOutlets: function(router, property) {
router.get('applicationController').connectOutlet('property', property);
},
}),
})
});
Run Code Online (Sandbox Code Playgroud)
这是我的模型:
App.Property = Ember.Object.extend({
id: null,
address: null,
address_2: null,
city: null,
state: null, …Run Code Online (Sandbox Code Playgroud) php ×3
ember.js ×2
javascript ×2
oop ×2
refactoring ×2
class-design ×1
codeigniter ×1
coding-style ×1
css ×1
html ×1
mysql ×1
subclass ×1